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...

Oyo Rooms Software Developer interview questions & answers

A Software Developer was asked Q. Given the head of a singly linked list and two integers m and n, reverse the nod...read more
A Software Developer was asked Q. Merge sort algorithm and divide array in the chunks
A Software Developer was asked Q. Position of Right Most Set Bit Determine the position of the rightmost set bit i...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. Given the head of a singly linked list and two integers m and n, reverse the nod...read more
A Software Developer was asked Q2. Merge sort algorithm and divide array in the chunks
A Software Developer was asked Q3. Position of Right Most Set Bit Determine the position of the rightmost set bit i...read more
Oyo Rooms 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