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
def solve ( n , vec ) : NEW_LINE INDENT a = [ ] NEW_LINE b = [ ] NEW_LINE for pr in vec : NEW_LINE INDENT a . append ( pr [ 0 ] ) NEW_LINE b . append ( pr [ 1 ] ) NEW_LINE DEDENT a . sort ( ) NEW_LINE b . sort ( ) NEW_LINE if ( ( n & 1 ) ) : NEW_LINE INDENT left = a [ n // 2 ] NEW_LINE right = b [ n // 2 ] NEW_LINE ans = right - left + 1 NEW_LINE DEDENT else : NEW_LINE INDENT left = ( a [ n // 2 ] + a [ n // 2 - 1 ] ) NEW_LINE right = ( b [ n // 2 ] + b [ n // 2 - 1 ] ) NEW_LINE ans = right - left + 1 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE vec = [ ( 100 , 100 ) , ( 10 , 10000 ) , ( 1 , 1000000000 ) ] NEW_LINE solve ( N , vec ) NEW_LINE DEDENT