Queries to check if sweets of given type can be eaten on given day or not | Java implementation of the above approach ; Function to find queries to check if a sweet of given type can be eaten on a given day or not ; Stores pairs { priority , quantity } of each sweet - type ; Sorting the order of sweets in decreasing order of their priority ; Stores the window { min_days , max_days } for each sweet - type ; Variables to calculate the windows ; Traverse the array ; Calculating maximum and minimum number of days required to complete the sweets of the current type ; Creating the window and storing it ; Traversing the queries ; x : Type of sweet , y : Day ; Find the window for the sweet of type x ; If the given day lies within the window ; Driver Code ; Quantites of sweets of each type ; Priorites of each type sweet ; Maximum sweet of one type that can be eaten on a day ; Queries ; Calculating number of types
import java . util . ArrayList ; import java . util . Arrays ; import java . util . Collections ; import java . util . Comparator ; import java . util . HashMap ; import java . util . Map ; class GFG { static class Pair < K , V > { K first ; V second ; public Pair ( K first , V second ) { this . first = first ; this . second = second ; } public static < K , V > Pair < K , V > of ( K first , V second ) { return new Pair < > ( first , second ) ; } } static void sweetTypeOnGivenDay ( int a [ ] , int b [ ] , int n , int k , ArrayList < Pair < Integer , Integer > > q ) { ArrayList < Pair < Integer , Pair < Integer , Integer > > > v = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) v . add ( Pair . of ( b [ i ] , Pair . of ( a [ i ] , i + 1 ) ) ) ; Collections . sort ( v , new Comparator < Pair < Integer , Pair < Integer , Integer > > > ( ) { @ Override public int compare ( Pair < Integer , Pair < Integer , Integer > > a , Pair < Integer , Pair < Integer , Integer > > b ) { if ( a . first == b . first ) { if ( a . second . first == b . second . first ) { return b . second . second - a . second . second ; } return b . second . first - a . second . first ; } return b . first - a . first ; } } ) ; Map < Integer , Pair < Integer , Integer > > mp = new HashMap < > ( ) ; int lowerbound = 0 , upperbound = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int maxi_days = v . get ( i ) . second . first ; int mini_days = v . get ( i ) . second . first / k ; if ( v . get ( i ) . second . first % k != 0 ) mini_days ++ ; upperbound += maxi_days ; mp . put ( v . get ( i ) . second . second , Pair . of ( lowerbound , upperbound ) ) ; lowerbound += mini_days ; } for ( int i = 0 ; i < q . size ( ) ; i ++ ) { int x = q . get ( i ) . first , y = q . get ( i ) . second ; int e = mp . get ( x ) . first ; int f = mp . get ( x ) . second ; if ( y >= e && y <= f ) System . out . println ( " Yes ▁ " ) ; else System . out . println ( " No ▁ " ) ; } } public static void main ( String [ ] args ) { int A [ ] = { 6 , 3 , 7 , 5 , 2 } ; int B [ ] = { 1 , 2 , 3 , 4 , 5 } ; int K = 3 ; ArrayList < Pair < Integer , Integer > > Queries = new ArrayList < > ( Arrays . asList ( Pair . of ( 4 , 4 ) , Pair . of ( 3 , 16 ) , Pair . of ( 2 , 7 ) ) ) ; int n = A . length ; sweetTypeOnGivenDay ( A , B , n , K , Queries ) ; } }