Queries to check if a number lies in N ranges of L | Function that answers every query ; container to store all range ; hash the L and R ; Push the element to container and hash the L and R ; sort the elements in container ; each query ; get the number same or greater than integer ; if it lies ; check if greater is hashed as 2 ; else check if greater is hashed as 1 ; Lower bound implementation ; Driver Code ; function call to answer queries
function answerQueries ( a , n , queries , q ) { let v = [ ] ; let mpp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { v . push ( a [ i ] [ 0 ] ) ; mpp . set ( a [ i ] [ 0 ] , 1 ) ; v . push ( a [ i ] [ 1 ] ) ; mpp . set ( a [ i ] [ 1 ] , 2 ) ; } v . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < q ; i ++ ) { let num = queries [ i ] ; let ind = lowerBound ( v , num ) ; if ( ind >= 0 && v [ ind ] == num ) document . write ( " " ) ; else { if ( ind >= 0 && mpp . get ( v [ ind ] ) == 2 ) document . write ( " " ) ; document . write ( " " ) ; } } } function lowerBound ( array , value ) { let low = 0 ; let high = array . length ; while ( low < high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; if ( value <= array [ mid ] ) { high = mid ; } else { low = mid + 1 ; } } return low ; } let a = [ [ 5 , 6 ] , [ 1 , 3 ] , [ 8 , 10 ] ] ; let n = 3 ; let queries = [ 2 , 3 , 4 , 7 ] ; let q = queries . length ; answerQueries ( a , n , queries , q ) ;