Last Updated: 16 Mar, 2021

Jar of Candies

Easy
Asked in companies
AdobeOla

Problem statement

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

Approaches

01 Approach

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 K from N.
  • If K is greater than N therefore, we can't give customer K candies, Hence return -1.