The median of a list of numbers is essentially it's middle element after sorting. The same number of elements occur after it as before. Given a list of numbers with an odd number of elements, can you find the median?
For example, the median of  is , the middle element in the sorted array.


Question from hackerrank.


Solution in C# - 


class Solution {

    static void Main()
    {
        int n = Convert.ToInt32(Console.ReadLine());
        string[] arr = Console.ReadLine().Trim().Split(' ');
        int[] num = new int[n];
        for(int i=0; i<n; i++)
            num[i] = Convert.ToInt32(arr[i]);
        Array.Sort(num);
        Console.WriteLine(num[n/2]);
    }

}

Post a Comment

Previous Post Next Post