Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Water and Jug problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Water and Jug problem solution, You are given two jugs with capacities jug1Capacity and jug2Capacity liters. There is an infinite amount of water supply available. Determine whether it is possible to measure exactly targetCapacity liters using these two jugs.

If targetCapacity liters of water are measurable, you must have targetCapacity liters of water contained within one or both buckets by the end.

Operations allowed:

  1. Fill any of the jugs with water.
  2. Empty any of the jugs.
  3. Pour water from one jug into another till the other jug is completely full, or the first jug itself is empty.
Leetcode Water and Jug problem solution

Leetcode Water and Jug problem solution in Python.

class Solution:
    def canMeasureWater(self, x: int, y: int, z: int) -> bool:
        x, y = (y, x) if y < x else (x, y)
        if x == z or y == z:
            return True
        if x == 0 or y == 0:
            return False 
        if z > x + y:
            return False
        if z % x == 0:
            return True
        if z == x + y:
            return True 
        if y % x == 0:
            return False
        if math.gcd(x,y) != 1 and z % math.gcd(x,y) != 0:
            return False
        if z % math.gcd(x,y) == 0:
            return True
        return True

Water and Jug problem solution in Java.

public boolean canMeasureWater(int x, int y, int z) {
        if(z>y && z>x)return false;
        if(z%gcd(x,y)==0)return true;
        return false;
        
    }
    public int gcd(int x,int y){
        if(y==0)return x;
        return gcd(y,x%y);
    }

Problem solution in C++.

class Solution {
public:
    bool canMeasureWater(int j1, int j2, int t, bool o = true) {
        if (o && j1+j2 <= t) return (j1+j2 == t);
        if (j1 > 0) return canMeasureWater(j2%j1, j1, t, false);
        return (t%j2 == 0);
    }
};

Problem solution in C.

int gcd(int a,int b)
{
if(a==0)
return b;
else if(b==0)
return a;
return gcd(b,a%b);

}
bool canMeasureWater(int x, int y, int z){

return z == 0 || (z <= x + y && z % gcd(x, y) == 0);
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes