Find the median

 Find the median


Problem Description


Given the marks of all students, calculate the median


Input:

The first line of input takes the number of test cases, T. Then T test cases follow. Each of the T test cases takes 2 input lines.The first line of each test case takes the number of students, N.The second line of each test case takes N space separated integers denoting the marks of all the students


Output:

Print the floor value of the median for each test case on a new line

Logic Test Case 1


Input (stdin)

2


4


56 67 30 79


4


78 89 67 76


Expected Output


61


77

Logic Test Case 2


Input (stdin)

2


3


34 44 67


5


87 44 32 69 74


Expected Output


44


69






CODE AREA

import java.io.*;

import java.util.*;

public class TestClass {

public static void main(String[] args) { 

Scanner s=new Scanner (System.in);

       int m=s.nextInt();

       int i;

       for (i=0;i<m;i++)

       { int n,c,j;

        n=s.nextInt();

        int a[]=new int[n];

         for (j=0;j<n;j++)

         {a[j]=s.nextInt();

         }

        Arrays.sort(a);

       if(n%2==0)

       { c=(a[n/2]+a[(n-1)/2])/2;

        }

        else

        { c=a[n/2];

        }

        System.out.println(c);

      }

}

}

Post a comment

0 Comments