365. Water and Jug Problem - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Problem Statement

You are given two jugs with capacities x and y liters respectively, and an infinite water supply. Your task is to determine whether it is possible to measure exactly z liters using these two jugs. You can perform operations such as filling a jug completely, emptying a jug, or pouring water from one jug to the other until either the first jug is empty or the second jug is full.

Examples

Example 1

Input: x = 3, y = 5, z = 4
Output: true
Explanation:
One possible solution is:

  1. Fill the 5-liter jug.
  2. Pour water from the 5-liter jug into the 3-liter jug until it is full. Now, you have 2 liters remaining in the 5-liter jug.
  3. Empty the 3-liter jug.
  4. Pour the 2 liters from the 5-liter jug into the 3-liter jug.
  5. Fill the 5-liter jug again.
  6. Pour water from the 5-liter jug into the 3-liter jug until it is full. The 3-liter jug already has 2 liters, so it needs only 1 liter more, leaving exactly 4 liters in the 5-liter jug.

Example 2

Input: x = 2, y = 6, z = 5
Output: false
Explanation:
It is impossible to measure exactly 5 liters with jug capacities of 2 and 6 liters under the given operations.

Constraints

  • The capacities x, y, and the target z are non-negative integers.
  • You have an infinite supply of water.
  • The operations allowed are filling a jug completely, emptying a jug, or pouring water from one jug to the other.

Hints

  • Think about the problem in terms of number theory. Specifically, any amount of water that can be measured using these operations is a linear combination of x and y.
  • Consider using Bézout's identity, which states that for any integers a and b, there exist integers m and n such that m * a + n * b = gcd(a, b). This means that any measurable quantity must be a multiple of the greatest common divisor (gcd) of x and y.
  • The target z must be less than or equal to the sum of the two jug capacities (i.e., z ≤ x + y).

Approach

The key observation is that the water jug problem is essentially a question of whether there exists a solution to the Diophantine equation:

  m * x + n * y = z

According to Bézout's identity, this equation has a solution in integers m and n if and only if z is a multiple of the gcd(x, y). However, even if z is a multiple of the gcd, you cannot measure more water than the sum of both jugs, so an additional constraint is z ≤ x + y.

Thus, the solution can be summarized in these steps:

  1. If z is 0, return true immediately (since you can have both jugs empty).
  2. If z is greater than x + y, return false because you cannot measure more water than the total capacity.
  3. Otherwise, calculate the gcd of x and y.
  4. Check if z is a multiple of the gcd. If yes, return true; otherwise, return false.

Detailed Explanation

Understanding Bézout's Identity

Bézout's identity states that for any integers x and y, there exist integers m and n such that:

  m * x + n * y = gcd(x, y)

This implies that the only amounts of water that can be measured using the jugs are multiples of gcd(x, y). Therefore, if z is not a multiple of gcd(x, y), there is no way to measure z liters using the operations provided.

Considering the Capacity Constraint

Even if z is a multiple of gcd(x, y), you must be able to physically hold z liters using the two jugs. Since you can only have at most x + y liters at any time (when both jugs are full), z must be less than or equal to x + y.

Putting It All Together

The algorithm can be implemented as follows:

  1. Check if z is 0. If yes, return true.

  2. Check if z > x + y. If yes, return false.

  3. Compute gcd(x, y).

  4. Check if z is divisible by the computed gcd. If it is, return true; otherwise, return false.

Python Code

Python3
Python3

. . . .

Java Code

Java
Java

. . . .

Complexity Analysis

  • Time Complexity:

    • The main computation is the calculation of the gcd, which runs in O(log(min(x, y))) time using the Euclidean algorithm.
  • Space Complexity:

    • O(1) extra space is used.

Common Pitfalls

  • Forgetting to check the case where z is 0.

  • Not verifying that z is less than or equal to the total capacity (x + y).

  • Not using the gcd to check the divisibility condition, which is central to determining if the water measurement is possible.

TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;