Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Water and Jug problem solution

YASH PAL, 31 July 2024

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

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

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

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes