what i did was to simply find the number before query …and then do as below-
Suppose the numbers are {11, 12, 13, 14, 15, 16} , and suppose 16 is the query, then find out 15 by binary search (the no less than k).
Then i will have a total of 4 numbers left to eat up(11 12 13 14) so 15 eats one to get to 16 … So now left with 3 … 14 eats 2 so we are left with (3-2-1)…including 14 also gone thereby total number being 16,15,14 …
(EDIT- From Vijju123- Basically what hes trying to say is that snake no. 15 will eat one snake and be of length 16, and 14 will eat 2 snakes and become of length 16.)
it works for repetitive elements also…then where am I going wrong can anyone help please…?
I have the same question.MY code was working for all type of test cases that, I could think of but still i was getting the wrong answer after submission.
for(int j=0;j<q;j++){
…
if(b[j]<=a[0]){
System.out.println(n);
break;
}
…
}
In the above code, you used break statement which would take the control out of the loop which has been used for processing the queries. So the next queries will not be processed and it will give you WA.
Instead of the break statement, you should use continue statement so that it can process the next queries also.This will correct your program but it will give you TLE.
For the below Input :