Search This Blog

Friday, December 6, 2013

Algorithm - Search - Find the Median

Find the Median

The following is the solution to Hacker Rank problem Find the Median using Java.  For solutions to other Hacker Rank Problem visit my page HackerRank, alternatively try searching for the problem in my blog.

Score: 10/10
/**
 *
 */


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

/**
 * @author Arun.G
 *
 */
public class Solution{

       /**
        * @param args
        */

       public static void main(String[] args) {
              /*
               * Enter your code here. Read input from STDIN. Print output to STDOUT.
               * Your class should be named Solution.
               */
              Scanner sc = new Scanner(System.in);
              int n = sc.nextInt();
              int[] ar = new int[n];
              for (int i = 0; i < n; i++) {
                     ar[i] = sc.nextInt();
              }
              Arrays.sort(ar);

              int median = ar[ar.length / 2];
              System.out.println(median);

              sc.close();
       }

}       

Labels