Asked inWolters Kluwer,Junior Engineer Associate
Water Jug Problem

You are given two water jugs with capacities X and Y litres respectively. Both the jugs are initially empty. There is an infinite amount of water supply available. The jugs do not have markings to measure smaller quantities.

The following operations are allowed:

• Fill any of the jugs entirely with water.
• Empty any of the jugs.
• Pour water from one jug into another till the other jug is full, or the first jug itself is empty.

You are required to tell whether it is possible to measure exactly ‘Z’ litres using both of the jugs.

If Z litres of water is measurable, you must have Z litres of water contained within one or both buckets by the end.

For example:

In order to measure 2 litres from jugs of 4 and 6 litres we can follow the following steps-

• Fill 6-litres jugs to its maximum capacity.
• Pour water from 6-litres jug to the jug with 4-litres capacity.
• Pour water from 6-litres jug to the jug with 4-litres capacity.
Input Format :
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test cases follow.

The first line of each test case contains three space-separated integers ‘X’, ‘Y’ and ‘Z’ denoting the capacities of both the jugs and the target measure, respectively.
Output Format :
For each test case, print True if we can measure the required value else, print False.

Output for each test case will be printed in a new line. 
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
 1 <= T <= 5 * 10^4
 0 <= X, Y, Z <= 5 * 10^4

Time Limit: 1 sec
AnswerBot
1y

The problem is to determine whether it is possible to measure a specific amount of water using two jugs with given capacities.

  • The jugs can be filled, emptied, and water can be poured from one jug to a...read more

CodingNinjas
author
2y
Using BFS

We will run a breadth-first search(BFS), keeping states as water present in both the jugs. We will visit all the states, keeping a hashmap for visited states to not revisit the same state. If...read more

CodingNinjas
author
2y
Using GCD

The above problem can be modelled by Diophantine equation ‘A’ * ‘X’ + ‘B’ * ‘Y’ = ‘Z’, where ‘A’ and ‘B’ are integers.

According to linear algebra, this equation is solvable if and only if th...read more

Add answer anonymously...
Wolters Kluwer Junior Engineer Associate Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter