My c++ code handling all cases with comments


  • 0
    T

    class Solution {

    int gcd(int x,int y)
    {
        return (!x ? y : gcd(y%x,x));
    }
    

    public:
    bool canMeasureWater(int x, int y, int z) {

        /* Trivial cases */
        if (!z) //empty measurement
            return true;
        if(z > x+y || (!x && !y)) // More than capacity or measuring capacities zero
            return false;
        if(x == z || y == z) // Same as desired
            return true;
    
        return (z % gcd(x,y) == 0);
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.