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
2mo
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!
Add answer anonymously...
TCS Software Engineer interview questions & answers
A Software Engineer was asked 1w agoQ. What are the SOLID design principles?
A Software Engineer was asked 2w agoQ. Write code to find prime numbers.
A Software Engineer was asked 1mo agoQ. What are the pillars of Object-Oriented Programming (OOP)?
Popular interview questions of Software Engineer
A Software Engineer was asked 1w agoQ1. What are the SOLID design principles?
A Software Engineer was asked 2w agoQ2. Write code to find prime numbers.
A Software Engineer was asked 3w agoQ3. What are pipes in Angular?
Top HR questions asked in TCS Software Engineer
A Software Engineer was asked 1mo agoQ1. Can you explain the project that excited you?
A Software Engineer was asked 2mo agoQ2. Which language are you proficient in?
A Software Engineer was asked 3mo agoQ3. What was your role in the college project?
Stay ahead in your career. Get AmbitionBox app
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