Ninja and Candies Problem

Ninja, a boy from Ninjaland, receives 1 coin every morning from his mother. He wants to purchase exactly 'N' candies. Each candy usually costs 2 coins, but it is available for 1 coin if on sale. Ninja aims to acquire exactly K[i] candies of the i-th type by purchasing them in the evening.

Ninja can buy any number of candies, including zero, of any type on any day, provided he has sufficient money. If candies are on sale, he can buy them at the discounted price of 1 coin; otherwise, the regular price is 2 coins.

In the candy shop, there are 'M' special sale offers. The j-th offer (D[j], C[j]) indicates that candies of the C[j]-th type are on sale on the D[j]-th day.

Ninja's goal is to purchase all candies as quickly as possible. Your task is to calculate the earliest day on which Ninja can buy all the candies.

Input:

The first line of input contains an integer ‘T’, which represents the number of test cases. Then, the test cases follow. Each test case begins with a line containing two space-separated integers, ‘N’ and ‘M’, which denote the total number of candy types and the number of special offers, respectively. The following line in each test case contains ‘N’ space-separated integers K[i], indicating the number of candies of each type that Ninja wants to buy. The next M lines contain two space-separated integers (D[i], C[i]), denoting that candies of the C[i]-th type are on sale on the D[i]-th day.

Output:

For each test case, print the minimum day by which Ninja can purchase all candies. Each result should appear on a new line.

Example:

Consider the following test case:

Input:
T = 1
N = 3, M = 3
K = [2, 3, 5]
Special Offers = [(1, 2), (3, 1), (5, 3)]
Output:
9

Constraints:

  • 1 <= T <= 10
  • 1 <= N, M <= 10^4
  • 0 <= K[i] <= 10^4
  • 1 <= C[i] <= N
  • 1 <= D[i] <= 10^4
  • The sum of all K[i] within a test case is less than 5000.

Note:

You don’t need to print anything explicitly; the task involves implementing the function to compute the answer.
AnswerBot
4mo

Calculate the earliest day on which Ninja can buy all candies with special offers.

  • Iterate through each day and check if any special offer is available for candies Ninja wants to buy

  • Keep track of the m...read more

Help your peers!
Select
Add answer anonymously...

Accenture Software Developer interview questions & answers

A Software Developer was asked 1w agoQ. Describe the SOLID principles.
A Software Developer was asked 4mo agoQ. What is polymorphism?
A Software Developer was asked 4mo agoQ. What design software do you know?

Popular interview questions of Software Developer

A Software Developer was asked 1w agoQ1. Describe the SOLID principles.
A Software Developer was asked 4mo agoQ2. What design software do you know?
A Software Developer was asked 4mo agoQ3. Define the tree data structure.

Top HR questions asked in Accenture Software Developer

A Software Developer was asked 1w agoQ1. Explain your projects.
A Software Developer was asked 1mo agoQ2. Why do you believe I am required in the company?
A Software Developer was asked 2mo agoQ3. What motivates you to choose a career in the software development field?
Accenture Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits