KatInterview

365. Water and Jug Problem

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

Method: Mathematical Approach using Bézout's Identity

1. Problem Analysis

Solution

Water and Jug Problem in Python

import math

class Solution:
    def canMeasureWater(self, x: int, y: int, target: int) -> bool:
        # If the target amount is greater than the total capacity of both jugs,
        # it's impossible to measure.
        if x + y < target:
            return False
        
        # According to Bézout's identity, any measurable amount of water must be a
        # linear combination of x and y, which means it must be a multiple of their
        # greatest common divisor (GCD).
        
        # Constraints are x, y >= 1, so gcd(x,y) is well-defined and positive.
        # The case of gcd(0,0) is handled for robustness, though not required by constraints.
        # math.gcd(0, 0) returns 0.
        if x == 0 and y == 0:
            return target == 0
            
        # Calculate the GCD.
        divisor = math.gcd(x, y)
        
        # The target is achievable if it's a multiple of the GCD.
        return target % divisor == 0

Water and Jug Problem in Java

class Solution {
    public boolean canMeasureWater(int x, int y, int target) {
        // If the target amount is greater than the total capacity of both jugs,
        // it's impossible to measure.
        if (x + y < target) {
            return false;
        }

        // According to Bézout's identity, the amount of water that can be measured
        // is a linear combination of x and y. All such amounts are multiples of gcd(x, y).
        // Let d = gcd(x, y). We can form any amount k*d as long as it's within the capacity.
        
        // Constraints are x, y >= 1, so gcd(x,y) will be well-defined and positive.
        // Handle the case where both jugs have 0 capacity for robustness.
        if (x == 0 && y == 0) {
            return target == 0;
        }

        // Calculate the greatest common divisor (GCD) of x and y.
        int divisor = gcd(x, y);

        // The target is achievable if it's a multiple of the GCD.
        return target % divisor == 0;
    }

    // Helper function to calculate GCD using the Euclidean algorithm.
    private int gcd(int a, int b) {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }
}

Water and Jug Problem in C++

class Solution {
public:
    bool canMeasureWater(int x, int y, int target) {
        // If the target amount is greater than the total capacity of both jugs,
        // it's impossible to measure.
        if (x + y < target) {
            return false;
        }

        // According to Bézout's identity, the amount of water that can be measured
        // is a linear combination of x and y. All such amounts are multiples of gcd(x, y).
        // Let d = gcd(x, y). We can form any amount k*d as long as it's within the capacity.
        
        // Constraints are x, y >= 1, so gcd(x,y) will be well-defined and positive.
        // We handle the gcd(0, 0) case just for completeness, though it's not needed here.
        if (x == 0 && y == 0) {
            return target == 0;
        }
        
        // Calculate the greatest common divisor (GCD) of x and y.
        int divisor = gcd(x, y);
        
        // The target is achievable if it's a multiple of the GCD.
        return target % divisor == 0;
    }

private:
    // Helper function to calculate GCD using the Euclidean algorithm.
    int gcd(int a, int b) {
        while (b) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }
};

Water and Jug Problem in TypeScript

/**
 * Calculates the greatest common divisor of two numbers using the Euclidean algorithm.
 * @param a The first number.
 * @param b The second number.
 * @returns The GCD of a and b.
 */
function gcd(a: number, b: number): number {
    while (b) {
        [a, b] = [b, a % b];
    }
    return a;
}

function canMeasureWater(x: number, y: number, target: number): boolean {
    // If the target amount is greater than the total capacity of both jugs,
    // it's impossible to measure.
    if (x + y < target) {
        return false;
    }

    // According to Bézout's identity, the amount of water that can be measured
    // is a linear combination of x and y. All such amounts are multiples of gcd(x, y).
    // Let d = gcd(x, y). We can form any amount k*d as long as it's within the capacity.
    
    // Constraints are x, y >= 1, so gcd(x,y) will be well-defined and positive.
    // Handle the case where both jugs have 0 capacity for robustness.
    if (x === 0 && y === 0) {
        return target === 0;
    }

    // Calculate the greatest common divisor (GCD) of x and y.
    const divisor = gcd(x, y);

    // The target is achievable if it's a multiple of the GCD.
    return target % divisor === 0;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.