Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public boolean canMeasureWater(int x, int y, int z) {
- //limit brought by the statement that water is finallly in one or both buckets
- if(x + y < z) return false;
- //case x or y is zero
- if( x == z || y == z || x + y == z ) return true;
- //get GCD, then we can use the property of Bézout's identity
- return z % gcd(x, y) == 0;
- }
- public int GCD(int a, int b){
- while(b != 0 ){
- int temp = b;
- b = a%b;
- a = temp;
- }
- return a;
- }
- public int gcd(int x, int y) {
- return y == 0 ? x : gcd(y, x % y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement