{x}
blog image

Water and Jug Problem

You are given two jugs with capacities x liters and y liters. You have an infinite water supply. Return whether the total amount of water in both jugs may reach target using the following operations:

  • Fill either jug completely with water.
  • Completely empty either jug.
  • Pour water from one jug into another until the receiving jug is full, or the transferring jug is empty.

 

Example 1:

Input: x = 3, y = 5, target = 4

Output: true

Explanation:

Follow these steps to reach a total of 4 liters:

  1. Fill the 5-liter jug (0, 5).
  2. Pour from the 5-liter jug into the 3-liter jug, leaving 2 liters (3, 2).
  3. Empty the 3-liter jug (0, 2).
  4. Transfer the 2 liters from the 5-liter jug to the 3-liter jug (2, 0).
  5. Fill the 5-liter jug again (2, 5).
  6. Pour from the 5-liter jug into the 3-liter jug until the 3-liter jug is full. This leaves 4 liters in the 5-liter jug (3, 4).
  7. Empty the 3-liter jug. Now, you have exactly 4 liters in the 5-liter jug (0, 4).

Reference: The Die Hard example.

Example 2:

Input: x = 2, y = 6, target = 5

Output: false

Example 3:

Input: x = 1, y = 2, target = 3

Output: true

Explanation: Fill both jugs. The total amount of water in both jugs is equal to 3 now.

 

Constraints:

  • 1 <= x, y, target <= 103

Solution Explanation: Water and Jug Problem

The problem asks whether it's possible to measure a target amount of water (target) using two jugs with capacities x and y. We can perform three operations: fill a jug, empty a jug, or pour water from one jug to another until one is full or empty.

This problem can be efficiently solved using the greatest common divisor (GCD) concept. The key insight is that you can only obtain volumes that are multiples of the GCD of the jug capacities (x and y).

Mathematical Approach:

Let's say gcd(x, y) represents the greatest common divisor of x and y. We can only measure amounts that are multiples of gcd(x, y). This is because any combination of pouring and filling can only result in volumes that are linear combinations of x and y. The linear combinations of two integers are always multiples of their GCD.

Therefore, it's possible to measure target if and only if:

  1. target is less than or equal to the sum of the jug capacities (x + y). This is because we can fill both jugs to their maximum capacity.
  2. target is a multiple of the greatest common divisor of x and y (gcd(x, y)).

Code Implementation (Python):

import math
 
def canMeasureWater(x: int, y: int, z: int) -> bool:
    """
    Determines if it's possible to measure z liters using jugs of capacity x and y.
 
    Args:
        x: Capacity of the first jug.
        y: Capacity of the second jug.
        z: Target amount of water.
 
    Returns:
        True if it's possible to measure z liters, False otherwise.
    """
    if z > x + y:  # Check if target exceeds the total capacity
        return False
    return z % math.gcd(x, y) == 0  # Check if target is a multiple of gcd(x, y)
 

Time and Space Complexity:

  • Time Complexity: O(log(min(x, y))). This is dominated by the math.gcd function, which typically uses the Euclidean algorithm, having logarithmic time complexity.
  • Space Complexity: O(1). The algorithm uses constant extra space.

Alternative Approach (DFS/BFS):

While the mathematical approach is far more efficient, the problem can also be solved using Depth-First Search (DFS) or Breadth-First Search (BFS). These approaches explore the state space of possible water levels in the jugs but are significantly less efficient. The provided DFS solutions in the original prompt demonstrate this approach. The time and space complexity of the DFS/BFS approaches are higher because they explore all possible combinations.

In summary, the most efficient solution leverages the mathematical properties of GCD, providing a concise and optimal solution to the Water and Jug Problem. The DFS/BFS solution is less efficient but provides an alternative way to understand the problem's logic.