[C++] Simple, Relative Prime based Soln


  • 0
    T
    class Solution {
    public:
        bool canMeasureWater(int x, int y, int z) {
            if(z==0) return true;
            if(x>y) swap(x,y);
    
            if(x+y<z) return false;
            if(x+y==z) return true;
            if(x==0 && y!=z) return false;
            if(x==1 && y>=z) return true;
            
            bool rp=true;
            for(int i=2; i<=x; i++){
                if(x%i==0 && y%i==0){
                    rp = false;
                    break;
                }
            }
    
            //Either relative primes
            if(rp) return true;
            //Or amount to make is a multiple of smaller container
            if(z%x==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.