Jar of Candies Problem Statement

You are given a jar containing candies with a maximum capacity of 'N'. The jar cannot have less than 1 candy at any point. Given 'K', the number of candies a customer wants, determine how many candies remain in the jar after providing the desired count.

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

Input:

T (number of test cases) 
For each test case:
N K (number of candies in the jar and number of candies requested)

Output:

The number of candies left in the jar or -1 if the request is invalid. Output each result on a new line for each test case.

Example:

Input:
1 
10 5
Output:
5
Explanation:

With 'N' = 10 and 'K' = 5, after giving away 5 candies, 5 candies remain in the jar.

Constraints:

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

Time Limit: 1 second

Note:

Focus on implementing the function; printing is managed elsewhere.
AnswerBot
4mo

Given a jar of candies with a maximum capacity, determine the number of candies left after providing the desired count to a customer.

  • Check if the number of candies requested is valid (less than or equ...read more

Help your peers!
Select
Add answer anonymously...

TCS Software Developer interview questions & answers

A Software Developer was asked 4w agoQ. What is the process flow for converting an infix expression to a postfix express...read more
A Software Developer was asked 4w agoQ. What is the process for synthesizing data to train a machine learning model with...read more
A Software Developer was asked 1mo agoQ. Write code to reverse a string.

Popular interview questions of Software Developer

A Software Developer was asked 1w agoQ1. What is Python?
A Software Developer was asked 3w agoQ2. What is the process for synthesizing data to train a machine learning model with...read more
A Software Developer was asked 1mo agoQ3. What is a string?

Top HR questions asked in TCS Software Developer

A Software Developer was asked 5d agoQ1. Can you provide an explanation of your project?
A Software Developer was asked 2mo agoQ2. Why should we choose you?
A Software Developer was asked 2mo agoQ3. What are your hobbies?
TCS 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