Search an element in given N ranges | Function to return the index of the range in which K lies and uses linear search ; Iterate and find the element ; If K lies in the current range ; K doesn 't lie in any of the given ranges ; Driver code
function findNumber ( a , n , K ) { for ( var i = 0 ; i < n ; i ++ ) { if ( K >= a [ i ] [ 0 ] && K <= a [ i ] [ 1 ] ) return i ; } return - 1 ; } var a = [ [ 1 , 3 ] , [ 4 , 7 ] , [ 8 , 11 ] ] ; var n = a . length ; var k = 6 ; var index = findNumber ( a , n , k ) ; if ( index != - 1 ) document . write ( index ) ; else document . write ( - 1 ) ;