Jar of Candies

There is a jar full of candies for sale at a mall counter. Jar has the capacity ‘N’. That is, jar can contain maximum ‘N’ candies when a jar is full. At any point in time jar can not have 0 or fewer candies.

Given the number ‘K’, the number of candies the customer wants. Your task is to return the candies left in the jar.

Note:

If 'K' is greater than 'N', return -1 denoting the invalid order.

For example:

Given if 'N' = 10, 'K' = 5. Then 5 candies are left in the jar after the event.

Input format:

The first line of input contains an integer ‘T’ denoting the number of test cases.

The first and the only line of each test case contains two space-separated integers N and K, where ‘N’ is the number of candies in jar and ‘K’ is the number of candies the customer requires.

Output format:

For each test case, return the candies left in the jar. If the order is invalid return -1.

The output of each test case will be printed in a separate line.

Note:

You don’t need to print anything; it has already been taken care of. You just need to implement the given function.

Constraints:

1 <= T <= 5
1 <= N, K <= 10^4

Time Limit: 1 second
CodingNinjas
author
2y
Comparing Candies

The main idea is to check if K is greater than N or not.

  • If K is lesser than N, therefore, we can give the customer K candies and return the remaining candies in the jar by subtracting...read more
Help your peers!
Add answer anonymously...
TCS Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter