Count distinct median possible for an Array using given ranges of elements | C # implementation to count the number of distinct medians of an array where each array elements are given by a range ; Function to count the number of distinct medians of an array where each array elements are given by a range ; Loop to store the starting and end range in the array ; Condition to check if the length of the array is odd ; Driver code ; Function call
using System ; using System . Collections ; using System . Collections . Generic ; public class Point { public int x , y ; public Point ( int xx , int yy ) { x = xx ; y = yy ; } } class GFG { static void solve ( int n , ArrayList vec ) { ArrayList a = new ArrayList ( ) ; ArrayList b = new ArrayList ( ) ; foreach ( Point pr in vec ) { a . Add ( pr . x ) ; b . Add ( pr . y ) ; } a . Sort ( ) ; b . Sort ( ) ; int left , right , ans ; if ( ( n & 1 ) != 0 ) { left = ( int ) a [ n / 2 ] ; right = ( int ) b [ n / 2 ] ; ans = right - left + 1 ; } else { left = ( ( int ) a [ n / 2 ] + ( int ) a [ n / 2 - 1 ] ) ; right = ( ( int ) b [ n / 2 ] + ( int ) b [ n / 2 - 1 ] ) ; ans = right - left + 1 ; } Console . WriteLine ( ans ) ; } static public void Main ( ) { int N = 3 ; ArrayList vec = new ArrayList ( ) ; vec . Add ( new Point ( 100 , 100 ) ) ; vec . Add ( new Point ( 10 , 10000 ) ) ; vec . Add ( new Point ( 1 , 1000000000 ) ) ; solve ( N , vec ) ; } }