Created
December 6, 2019 07:24
-
-
Save zeraf29/a51ac8a44424c8b99d1cf239f90124e1 to your computer and use it in GitHub Desktop.
find numbers which can be divided by integers of array A and can divide evenly into integers of array B.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static int getGcd(int num1, int num2) { | |
| int temp = 0; | |
| while(num2>0){ | |
| temp = num1 % num2; | |
| num1 = num2; | |
| num2 = temp; | |
| } | |
| return num1; | |
| } | |
| private static int getLcm(int num1, int num2) { | |
| return num1*num2/getGcd(num1,num2); | |
| } | |
| public static int getTotalX(List<Integer> a, List<Integer> b) { | |
| // Write your code here | |
| int tempLcm = 0; | |
| if(a.size()==1) tempLcm = a.get(0); | |
| else { | |
| tempLcm = getLcm(a.get(0),a.get(1)); | |
| for(int i=2; i<a.size(); i++) { | |
| tempLcm = getLcm(tempLcm, a.get(i)); | |
| } | |
| } | |
| int tempGcd = 0; | |
| if(b.size()==1) tempGcd =b.get(0); | |
| else { | |
| tempGcd = getGcd(b.get(0),b.get(1)); | |
| for(int i=2; i<b.size(); i++) { | |
| tempGcd = getGcd(tempGcd, b.get(i)); | |
| } | |
| } | |
| int cnt = 0; | |
| for(int i=tempLcm; i<=tempGcd; i+=tempLcm ) { | |
| if(tempGcd%i==0) cnt++; | |
| } | |
| return cnt; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment