Problem Link : BURGER
Approach : Do what exactly problem want -
- If Y is not divisible by X then answer is -1 .why (think about this)
- Now for other cases -
we have to subtract the sum of all X * (2i) until my (Y > (2i) ) [for all i>=0 ] now increase the count of βiβ into map .
we have two situation left either my Y is zero , or Y is something remaining.
if Y is zero then we have to print answer.
for other case do the same as above ie., we have to subtract the sum of all X * (2i) until my (Y > (2i) ) [for all i>=0 ] now increase the count of βiβ into map .
but note that if we rest then we have to increase the answer by 1 , as in question it is mentioned that " Chef also allows you take a break from eating for exactly 1 minute."
Now for printing the answer we have to check whether in our map the frequency of each element (basically that βiβ which is stored ) is one or not . if it is more than one that means we have 2 same time of eating streaks and in question it is clearly mentioned that chef requires all unique time streaks
[You can check this by using set too]
Letβs take sample test case-
X = 1 , Y = 7
Now -
1st minute = X ie, 1 => X * 20 = 1 * 2 0 = 1
2nd minute = 2X ie, 2 => X * 21 = 1 * 2 1 = 2
3rd minute = 4X ie, 4 => X * 22 = 1 * 2 2 = 4
in one go how many minutes we take 3 right ?
now while printing check 3 occurs only once β¦yeah it occur only once .
1 + 2 + 4 = 7 , required minutes is 3.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
X = 1 , Y = 4
Now -
1st minute = X ie, 1 => X * 20 = 1 * 2 0 = 1
2nd minute = 2X ie, 2 => X * 21 = 1 * 2 1 = 2
2 minutes streak ,
β>> now wait for 1 min because if we subtract 4 (which is from 3 minute then Y will be -ve) [ do not store this into map]
start again
1st minute = X ie, 1 => X * 20 = 1 * 2 0 = 1
1 min streak .
map > [ 2 β 1 times , 1 β 1 times ] ,all are occur once print answer -
1 + 2 + 1 = 4 , required minutes is 3 + 1 [that rest minute]
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Now take an example in which Y is divisible by X but still answer is -1
X = 2 , Y = 12
Now -
1st minute = X ie, 1 => X * 20 = 2 * 2 0 = 2
2nd minute = 2X ie, 2 => X * 21 = 2 * 2 1 = 4
2 minutes streak ,
β>> now wait for 1 min because if we subtract (2*4) (which is from 3 minute then Y will be -ve) [ do not store this into map]
start again
1st minute = X ie, 1 => X * 20 = 2 * 2 0 = 2
2nd minute = 2X ie, 2 => X * 21 = 2 * 2 1 = 4
2 minutes streak .
map > [ 2 β 2 times ] , ohhhoo , 2 minutes streak occur 2 times β¦ shit chef need unique streaks
answer is -1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
My solution : βCodeChef: Practical coding for everyoneβ