Search an element in given N ranges | Function to return the index of the range in which K lies and uses binary search ; Binary search ; Find the mid element ; If element is found ; Check in first half ; Check in second half ; Not found ; Driver code
def findNumber ( a , n , K ) : NEW_LINE INDENT low = 0 NEW_LINE high = n - 1 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) >> 1 NEW_LINE if ( K >= a [ mid ] [ 0 ] and K <= a [ mid ] [ 1 ] ) : NEW_LINE INDENT return mid NEW_LINE DEDENT elif ( K < a [ mid ] [ 0 ] ) : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT a = [ [ 1 , 3 ] , [ 4 , 7 ] , [ 8 , 11 ] ] NEW_LINE n = len ( a ) NEW_LINE k = 6 NEW_LINE index = findNumber ( a , n , k ) NEW_LINE if ( index != - 1 ) : NEW_LINE INDENT print ( index ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT