Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int solution100Gcd(int[] A, int[] B){
- int r = 0;
- for(int i = 0; i < A.length; i++){
- int a = A[i], b = B[i];
- if(a == b){
- r++;
- } else {
- int gcd = gcd(a, b);
- if( restFromDividingByGcd(a, gcd) == 1 &&
- restFromDividingByGcd(b, gcd) == 1){
- r++;
- }
- }
- }
- return r;
- }
- int restFromDividingByGcd(int number, int gcd_value){
- while(gcd_value != 1){
- number /= gcd_value;
- gcd_value = gcd(number, gcd_value);
- }
- return number;
- }
- int gcd(int a, int b) {
- if(a % b == 0)
- return b;
- else
- return gcd(b, a % b);
- }
Add Comment
Please, Sign In to add comment