Ninja And Candies
Ninja is a boy who lives in ninjaland. Every day, during the morning he gets 1 coin from his mother. He wants to buy exactly ‘N’ candies. Each of the candies cost 2 coins usually and 1 coin if it is on sale. Ninja has to buy exactly K[i] candies of the i-th type(he buys candies in the evening).
Ninjas can buy any(possibly zero) number of candies of any type during any day(if he has enough money to do it). If the candy is on sale he can buy it for 1 coin and otherwise he has to buy it for 2 coins.
There are ‘M’ special offers in the candy shop. The j-th offer (D[j], C[j]) means that candies of the C[j]-th type are on sale during the D[j]-th day.
Ninja wants to buy all candies as soon as possible. Your task is to calculate the minimum day when he can buy all the candies.
Input Format
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test cases follow.
The first line of each test case contains two space separated integers ‘N’ and ‘M’ denoting the total number of types of candies and the number of special offers in the candy shop.
The second line of each test case contains ‘N’ space-separated integers K[i], denoting the number of candies of each type Ninja has to order.
The next M-lines contain two space-separated integers (D[i], C[i]), denoting that the candy of C[i] type is on sale on the D[i]-th day.
Output Format :
For each query, print the minimum day when the Ninja can order all candies.
Output for each test case will be printed in a new line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N, M <= 10^4
0 <= K[i] <= 10^4
1 <= C[i] <= N
1 <= D[i] <= 10^4
Here N denotes the total number of candies and M denotes the total number of special offers.
Here K[i] denotes the number of candies of the i-th type Ninjas has to order.
Here C[i] and D[i] denote that the candy if the C[i]-th type will be on sale on the D[i]-th day.
Sum of K[i]’s in each test case will be less than 5000.
CodingNinjas
author
2y
Using Linear Search
We will iterate over all possible days, which will be between 1 to 2*(sum of all K[i]). Let our current day be ‘currDay’. We will check if any valid distribution is possible for the...read more
CodingNinjas
author
2y
Using Binary Search
To solve this problem, we can use binary search over the answer. The key observations are-
- If we can buy every candy in ‘totDays’ days, then we can also buy it in days D > totDays.
- S...read more
Help your peers!
Add answer anonymously...
Top Accenture Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Accenture Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app