@@ -0,0 +1,1640 @@
+package com.codeprep.findmedian;%0A%0Aimport java.io.IOException;%0Aimport java.util.Scanner;%0A%0Apublic class Solution %7B%0A%0A%09// Complete the findMedian function below.%0A%09static int findMedian(int%5B%5D arr) %7B%0A%0A%09%09sort(arr, 0, arr.length - 1);%0A%09%09return arr%5Barr.length / 2%5D;%0A%09%7D%0A%0A%09private static int partition(int arr%5B%5D, int low, int high) %7B%0A%09%09int pivot = arr%5Bhigh%5D;%0A%09%09int i = (low - 1); // index of smaller element%0A%09%09for (int j = low; j %3C high; j++) %7B%0A%09%09%09// If current element is smaller than the pivot%0A%09%09%09if (arr%5Bj%5D %3C pivot) %7B%0A%09%09%09%09i++;%0A%0A%09%09%09%09// swap arr%5Bi%5D and arr%5Bj%5D%0A%09%09%09%09int temp = arr%5Bi%5D;%0A%09%09%09%09arr%5Bi%5D = arr%5Bj%5D;%0A%09%09%09%09arr%5Bj%5D = temp;%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09// swap arr%5Bi+1%5D and arr%5Bhigh%5D (or pivot)%0A%09%09int temp = arr%5Bi + 1%5D;%0A%09%09arr%5Bi + 1%5D = arr%5Bhigh%5D;%0A%09%09arr%5Bhigh%5D = temp;%0A%0A%09%09return i + 1;%0A%09%7D%0A%0A%09private static void sort(int arr%5B%5D, int low, int high) %7B%0A%09%09if (low %3C high) %7B%0A%09%09%09/*%0A%09%09%09 * pi is partitioning index, arr%5Bpi%5D is now at right place%0A%09%09%09 */%0A%09%09%09int pi = partition(arr, low, high);%0A%0A%09%09%09// Recursively sort elements before%0A%09%09%09// partition and after partition%0A%09%09%09sort(arr, low, pi - 1);%0A%09%09%09sort(arr, pi + 1, high);%0A%09%09%7D%0A%09%7D%0A%0A%09private static final Scanner scanner = new Scanner(System.in);%0A%0A%09public static void main(String%5B%5D args) throws IOException %7B%0A%0A%09%09int n = scanner.nextInt();%0A%09%09scanner.skip(%22(%5Cr%5Cn%7C%5B%5Cn%5Cr%5Cu2028%5Cu2029%5Cu0085%5D)?%22);%0A%0A%09%09int%5B%5D arr = new int%5Bn%5D;%0A%0A%09%09String%5B%5D arrItems = scanner.nextLine().split(%22 %22);%0A%09%09scanner.skip(%22(%5Cr%5Cn%7C%5B%5Cn%5Cr%5Cu2028%5Cu2029%5Cu0085%5D)?%22);%0A%0A%09%09for (int i = 0; i %3C n; i++) %7B%0A%09%09%09int arrItem = Integer.parseInt(arrItems%5Bi%5D);%0A%09%09%09arr%5Bi%5D = arrItem;%0A%09%09%7D%0A%0A%09%09int result = findMedian(arr);%0A%0A%09%09System.out.println(String.valueOf(result));%0A%09%09scanner.close();%0A%09%7D%0A%7D

solution to the find the median problem