Queries to check if a number lies in N ranges of L | Java program to check if the number lies in given range ; 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
import java . io . * ; import java . util . * ; class GFG { static void answerQueries ( int [ ] [ ] a , int n , int [ ] queries , int q ) { Vector < Integer > v = new Vector < > ( ) ; HashMap < Integer , Integer > mpp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { v . add ( a [ i ] [ 0 ] ) ; mpp . put ( a [ i ] [ 0 ] , 1 ) ; v . add ( a [ i ] [ 1 ] ) ; mpp . put ( a [ i ] [ 1 ] , 2 ) ; } Collections . sort ( v ) ; for ( int i = 0 ; i < q ; i ++ ) { int num = queries [ i ] ; int ind = lowerBound ( v , num ) ; if ( ind >= 0 && v . elementAt ( ind ) == num ) System . out . println ( " Yes " ) ; else { if ( ind >= 0 && mpp . get ( v . elementAt ( ind ) ) == 2 ) System . out . println ( " Yes " ) ; System . out . println ( " No " ) ; } } } static int lowerBound ( Vector < Integer > array , int value ) { int low = 0 ; int high = array . size ( ) ; while ( low < high ) { final int mid = ( low + high ) / 2 ; if ( value <= array . elementAt ( mid ) ) { high = mid ; } else { low = mid + 1 ; } } return low ; } public static void main ( String [ ] args ) { int [ ] [ ] a = { { 5 , 6 } , { 1 , 3 } , { 8 , 10 } } ; int n = 3 ; int [ ] queries = { 2 , 3 , 4 , 7 } ; int q = queries . length ; answerQueries ( a , n , queries , q ) ; } }