Count distinct median possible for an Array using given ranges of elements | 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
function solve ( n , vec ) { let a = [ ] ; let b = [ ] ; for ( let pr = 0 ; pr < vec . length ; pr ++ ) { a . push ( vec [ pr ] [ 0 ] ) ; b . push ( vec [ pr ] [ 1 ] ) ; } a . sort ( function ( c , d ) { return c - d ; } ) ; b . sort ( function ( c , d ) { return c - d ; } ) ; let left , right , ans ; if ( ( n & 1 ) != 0 ) { left = a [ Math . floor ( n / 2 ) ] ; right = b [ Math . floor ( n / 2 ) ] ; ans = right - left + 1 ; } else { left = ( a [ Math . floor ( n / 2 ) ] + a [ Math . floor ( n / 2 ) - 1 ] ) ; right = ( b [ Math . floor ( n / 2 ) ] + b [ Math . floor ( n / 2 ) - 1 ] ) ; ans = right - left + 1 ; } document . write ( ans ) ; } let N = 3 ; let vec = [ ] ; vec . push ( [ 100 , 100 ] ) ; vec . push ( [ 10 , 10000 ] ) ; vec . push ( [ 1 , 1000000000 ] ) ; solve ( N , vec ) ;