Queries to check if any pair exists in an array having values at most equal to the given pair | Function that performs binary search to find value less than or equal to first value of the pair ; Perform binary search ; Find the mid ; Update the high ; Else update low ; Return the low index ; Function to modify the second value of each pair ; start from index 1 ; Function to evaluate each query ; Find value less than or equal to the first value of pair ; check if we got the required pair or not ; Function to find a pair whose values is less than the given pairs in query ; Find the size of the vector ; sort the vector based on the first value ; Function Call to modify the second value of each pair ; Traverse each queries ; Evaluate each query ; Print the result ; Driver function ; Function Call
function binary_search ( vec , a , n ) { let low , high , mid ; low = 0 ; high = n - 1 ; while ( low < high ) { mid = low + Math . floor ( ( high - low + 1 ) / 2 ) ; if ( vec [ mid ] [ 0 ] > a ) { high = mid - 1 ; } else if ( vec [ mid ] [ 1 ] <= a ) { low = mid ; } } return low ; } function modify_vec ( v , n ) { for ( let i = 1 ; i < n ; i ++ ) { v [ i ] [ 1 ] = Math . min ( v [ i ] [ 1 ] , v [ i - 1 ] [ 1 ] ) ; } } function evaluate_query ( v , n , m1 , m2 ) { let temp = binary_search ( v , n , m1 ) ; if ( ( v [ temp ] [ 0 ] <= m1 ) && ( v [ temp ] [ 1 ] <= m2 ) ) { return 1 ; } return 0 ; } function checkPairs ( v , queries ) { let n = v . length ; v . sort ( function ( a , b ) { return a [ 0 ] - b [ 0 ] } ) ; modify_vec ( v , n ) ; let k = queries . length ; for ( let i = 0 ; i < k ; i ++ ) { let m1 = queries [ i ] [ 0 ] ; let m2 = queries [ i ] [ 1 ] ; let result = evaluate_query ( v , n , m1 , m2 ) ; if ( result > 0 ) document . write ( " " ) ; else document . write ( " " ) ; } } let arr = [ [ 3 , 5 ] , [ 2 , 7 ] , [ 2 , 3 ] , [ 4 , 9 ] ] ; let queries = [ [ 3 , 4 ] , [ 3 , 2 ] , [ 4 , 1 ] , [ 3 , 7 ] ] ; checkPairs ( arr , queries ) ;