You are given two jugs with capacities x and y litres. There is an infinite amount of water supply available.
You need to determine whether it is possible to measure exactly z litres using these two jugs.

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


Operations allowed:

Fill any of the jugs completely 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.



Example 1: (From the famous "Die Hard" example)

Input: x = 3, y = 5, z = 4
Output: True



Example 2:

Input: x = 2, y = 6, z = 5
Output: False



Credits:Special thanks to @vinod23 for adding this problem and creating all test cases.

Difficulty: interview

Starter code:

class Solution:
    def canMeasureWater(self, x: int, y: int, z: int) -> bool:
        

Solutions:
class Solution:
     def canMeasureWater(self, x, y, z):
         """
         :type x: int
         :type y: int
         :type z: int
         :rtype: bool
         """
         if x > y:
             x, y = y, x
         if z < 0 or z > x+y:
             return False
         if x == 0:
             return z == y or z == 0
         if z % x == 0:
             return True
         if y % x == 0:
             return False
         a = x
         b = y%x
         while a > 1 and b > 1:
             a = a%b
             a, b = b, a
         if b == 0:
             m = a
         else:
             m = b
         if z%m == 0:
             return True
         return False

class Solution:
     def canMeasureWater(self, x, y, z):
         """
         :type x: int
         :type y: int
         :type z: int
         :rtype: bool
         """        
         def gcd(x, y):
             if y == 0: return x
             return gcd(y, x % y)
         
         if x == 0 and y == 0: return z == 0
         return z % gcd(x, y) == 0 and x + y >= z

class Solution:
     def canMeasureWater(self, x, y, z):
         """
         :type x: int
         :type y: int
         :type z: int
         :rtype: bool
         """
         if x + y < z: return False
         if x == z or y == z or x + y == z: return True
         while y:
             x, y = y, x % y
         return not z % x


class Solution:
     def canMeasureWater(self, x, y, z):
         """
         :type x: int
         :type y: int
         :type z: int
         :rtype: bool
         """
         if not x or not y:  # (if x == 0 or y == 0): return (z == 0)
             return not z
         if z > x + y:
             return False
         
         # greatest common divisor --> Euclidian Algorithm
         while x != y:
             if x > y:
                 x, y = x - y, y
             else:
                 x, y = x, y - x
         
         return False if z % x else True

class Solution:
     def canMeasureWater(self, x, y, z):
         """
         :type x: int
         :type y: int
         :type z: int
         :rtype: bool
         """
         if x + y < z: return False
         gcd = lambda a, b: (gcd(b, a % b) if a % b else b)
         return z == 0 or z % gcd(x, y) == 0

class Solution:
     def canMeasureWater(self, x, y, z):
         """
         :type x: int
         :type y: int
         :type z: int
         :rtype: bool
         """
         
         a,b=x,y
         while y:
             r=x%y
             x=y
             y=r
         return bool(not z or (x and z<=a+b and not z%x))

class Solution:
     def canMeasureWater(self, x, y, z):
         """
         :type x: int
         :type y: int
         :type z: int
         :rtype: bool
         """
         if z in [0, x, y, x + y]:
             return True
         if z > x + y or z < 0:
             return False
         if x > y:
             x, y = y, x
         if x != 0:
             temp = z % x
             for i in range(x):
                 if temp == (y * i) % x:
                     return True
         if y != 0:
             temp = z % y
             for i in range(y):
                 if temp == (x * i) % y:
                     return True
         return False

class Solution:
     def canMeasureWater(self, x, y, z):
         """
         :type x: int
         :type y: int
         :type z: int
         :rtype: bool
         """
         if z in [0, x, y, x + y]:
             return True
         if z > x + y or z < 0:
             return False
         if x > y:
             x, y = y, x
         if x != 0:
             for i in range(x):
                 if z % x == (y * i) % x:
                     return True
         if y != 0:
             for i in range(y):
                 if z % y == (x * i) % y:
                     return True
         return False