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:


	Fill any of the jugs with water.
	Empty any of the jugs.
	Pour water from one jug into another till the other jug is completely full, or the first jug itself is empty.


&nbsp;
Example 1:

Input: jug1Capacity = 3, jug2Capacity = 5, targetCapacity = 4
Output: true
Explanation: The famous Die Hard example 


Example 2:

Input: jug1Capacity = 2, jug2Capacity = 6, targetCapacity = 5
Output: false


Example 3:

Input: jug1Capacity = 1, jug2Capacity = 2, targetCapacity = 3
Output: true


&nbsp;
Constraints:


	1 &lt;= jug1Capacity, jug2Capacity, targetCapacity &lt;= 106


class Solution:
    def canMeasureWater(self, jug1Capacity: int, jug2Capacity: int, targetCapacity: int) -> bool:
        n1, n2, t = jug1Capacity, jug2Capacity, targetCapacity
        if n1 == t or n2 == t or n1 + n2 == t:
            return True
        if n1 + n2 < t:
            return False
        if n1 < n2:
            n1, n2 = n2, n1
        stack = []
        visited = set()
        d = n1 - n2
        if d == t:
            return True
        while d > n2:
            d -= n2
            if d == t:
                return True
        stack.append(d)
        while stack:
            #print(stack)
            d = stack.pop()
            visited.add(d)
            n = n1 + d
            if n == t:
                return True
            n = n1 - d
            if n == t:
                return True
            while n > n2:
                n -= n2
                if n == t:
                    return True
            if n < n2 and n not in visited:
                stack.append(n)
            n = n2 - d
            if n == t:
                return True
            if n not in visited:
                stack.append(n)
        return False
class Solution {
    private static int gcd(int a,int b){
        if(b==0)return a;
        return gcd(b,a%b);
    }
    public boolean canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) {
        if(targetCapacity>jug1Capacity+jug2Capacity){
            return false;
        }
        int g=gcd(jug1Capacity,jug2Capacity);
        return (targetCapacity%g==0);
    }
}
class Solution {
public:
    bool canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) {
        
        if(targetCapacity > jug1Capacity + jug2Capacity)
            return false;
        
        vector<int> dp(jug1Capacity + jug2Capacity + 1, -1);
        return helper(0, jug1Capacity, jug2Capacity, targetCapacity, dp);
    }
    
    bool helper(int tmp, int &jug1Capacity, int &jug2Capacity, int &targetCapacity, vector<int> &dp)
    {
        if(tmp < 0 || tmp > jug1Capacity + jug2Capacity)
            return false;
        
        if(tmp == targetCapacity)
            return true;
        
        if(dp[tmp] != -1)
            return dp[tmp];
        
        dp[tmp] = false;            
        if(helper(tmp + jug1Capacity, jug1Capacity, jug2Capacity, targetCapacity, dp))
            return dp[tmp] = true;
        
        if(helper(tmp - jug1Capacity, jug1Capacity, jug2Capacity, targetCapacity, dp))
            return dp[tmp] = true;
        
        if(helper(tmp + jug2Capacity, jug1Capacity, jug2Capacity, targetCapacity, dp))
            return dp[tmp] = true;
        
         if(helper(tmp - jug2Capacity, jug1Capacity, jug2Capacity, targetCapacity, dp))
            return dp[tmp] = true;
            
        return dp[tmp] = false;
    }
};
var canMeasureWater = function(jug1Capacity, jug2Capacity, targetCapacity) {
	const gcd = (x, y) => y === 0 ? x : gcd(y, x % y);

	return jug1Capacity + jug2Capacity >= targetCapacity && 
		targetCapacity % gcd(jug1Capacity, jug2Capacity) === 0;
};
Water and Jug Problem