Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. class Solution {
  2. public boolean canMeasureWater(int x, int y, int z) {
  3. //limit brought by the statement that water is finallly in one or both buckets
  4. if(x + y < z) return false;
  5. //case x or y is zero
  6. if( x == z || y == z || x + y == z ) return true;
  7.  
  8. //get GCD, then we can use the property of Bézout's identity
  9. return z % gcd(x, y) == 0;
  10. }
  11.  
  12. public int GCD(int a, int b){
  13. while(b != 0 ){
  14. int temp = b;
  15. b = a%b;
  16. a = temp;
  17. }
  18. return a;
  19. }
  20.  
  21. public int gcd(int x, int y) {
  22. return y == 0 ? x : gcd(y, x % y);
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement