C++ solution using gcd


  • -1
    Z
    int gcd(int a, int b)
    {
        if(b==0)return a;
        return gcd(b,a%b);
    }
    bool canMeasureWater(int x, int y, int z) {
        int tmp=0;
         if(z > x + y) return false;
        if(x<y)
        {
            tmp=x;
            x=y;
            y=tmp;
        }
        int t=gcd(x,y);
        if(z==0)return true;
        if(t==0)return false;
        if(z%t==0)return true;
        return false;
    }

Log in to reply
 

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