Water Jug Problem Statement

You have two water jugs with capacities X and Y liters respectively, both initially empty. You also have an infinite water supply. The goal is to determine if it is possible to measure exactly Z liters using these two jugs and the operations outlined below.

Operations Allowed:

• Fill any of the jugs completely with water.
• Empty any of the jugs.
• Pour water from one jug into another until the other jug is full or the first jug is empty.

Input:

The input consists of multiple test cases. The first line contains an integer 'T' representing the number of test cases. The next T lines each contain three space-separated integers X, Y, and Z indicating the capacities of both jugs and the target measurement, respectively.

Output:

For each test case, output 'True' if it's possible to measure exactly Z liters, otherwise output 'False'. Each result should be on a new line.

Example:

To measure 2 liters using jugs with capacities 4 and 6 liters:

  • Fill the 6-liter jug completely.
  • Transfer from the 6-liter jug to the 4-liter jug until the latter is full.
  • Repeat the transfer from the 6-liter jug to the 4-liter jug.

Constraints:

  • 1 ≤ T ≤ 5 * 104
  • 0 ≤ X, Y, Z ≤ 5 * 104
  • Time Limit: 1 second
Note:
You do not need to print anything; the printing is handled externally. Simply implement the required function.
AnswerBot
4mo

The Water Jug Problem involves determining if a specific amount of water can be measured using two jugs of different capacities.

  • Start by considering the constraints and limitations of the problem.

  • Thin...read more

Help your peers!
Select
Add answer anonymously...

TCS Software Engineer interview questions & answers

A Software Engineer was asked 3d agoQ. What is GenAI?
A Software Engineer was asked 3d agoQ. What is DevOps?
A Software Engineer was asked 2mo agoQ. What are the SOLID design principles?

Popular interview questions of Software Engineer

A Software Engineer was asked 2mo agoQ1. What are the SOLID design principles?
A Software Engineer was asked 2mo agoQ2. What is a data type?
A Software Engineer was asked 2mo agoQ3. Write code to find prime numbers.

Top HR questions asked in TCS Software Engineer

A Software Engineer was asked 2mo agoQ1. Can you explain the project that excited you?
A Software Engineer was asked 2mo agoQ2. Why do you want to join this job?
A Software Engineer was asked 3mo agoQ3. How does the role fit your career plans?
TCS Software Engineer 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