We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I've updated my code for better readability: I guees this will explain it a lot better.
importjava.io.*;importjava.util.*;importjava.text.*;importjava.math.*;importjava.util.regex.*;publicclassSolution{privatebooleancanADivideI(inti,int[]a,intn){booleananswer=false;for(intj=0;j<n;j++){if(i%a[j]==0){if(j==n-1){answer=true;}}else{j=n;}}returnanswer;}privatebooleancanIDivideB(inti,int[]b,intm){booleananswer=false;for(intk=0;k<m;k++){if(b[k]%i==0){if(k==m-1){answer=true;}}else{k=m;}}returnanswer;}publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);Solutions=newSolution();intn=in.nextInt();intm=in.nextInt();int[]a=newint[n];intmax=100,min=0,elementsFound=0;for(inta_i=0;a_i<n;a_i++){a[a_i]=in.nextInt();//Look for Max in A and set that as the MIN if(min<a[a_i]){min=a[a_i];}}int[]b=newint[m];for(intb_i=0;b_i<m;b_i++){b[b_i]=in.nextInt();//Look for Min in B and set that as the MAXif(max>b[b_i]){max=b[b_i];}}for(inti=min;i<=max;i++){if(s.canADivideI(i,a,n)&&s.canIDivideB(i,b,m)){elementsFound++;}}System.out.println(elementsFound);}}
I did something simmilar, I guess it is an optimized version.
importjava.io.*;importjava.util.*;importjava.text.*;importjava.math.*;importjava.util.regex.*;publicclassSolution{publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);intn=in.nextInt();intm=in.nextInt();int[]a=newint[n];intmax=100,min=0,answr=0;for(inta_i=0;a_i<n;a_i++){a[a_i]=in.nextInt();//Look for Max in A and set that as the MIN if(min<a[a_i]){min=a[a_i];}}int[]b=newint[m];for(intb_i=0;b_i<m;b_i++){b[b_i]=in.nextInt();//Look for Min in B and set that as the MAXif(max>b[b_i]){max=b[b_i];}}for(inti=min;i<=max;i++){for(intj=0;j<n;j++){if(i%a[j]==0){if(j==n-1){for(intk=0;k<m;k++){if(b[k]%i==0){if(k==m-1){answr++;}}else{k=m;}}}}else{j=n;}}}System.out.println(answr);}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Between Two Sets
You are viewing a single comment's thread. Return to all comments →
EDIT - 1:
I've updated my code for better readability: I guees this will explain it a lot better.
I did something simmilar, I guess it is an optimized version.