HackerRank C- Between Two Sets




Complete the getTotalX function in the editor below. It should return the number of integers that are betwen the sets.

getTotalX has the following parameter(s):

a: an array of integers
b: an array of integers



  1. #include<stdio.h>
  2. int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } int lcm(int x, int y ) {
  3. if(x==0)
  4. return 0;
  5. return (x*y)/gcd(x,y);
  6. }
  7. int main() { int n,m; scanf("%d %d",&n,&m); int a[n],b[m];
  8. for(int i=0; i<n; i++)
  9. {
  10. scanf("%d",&a[i]);
  11. }
  12. for(int j=0; j<m; j++)
  13. {
  14. scanf("%d",&b[j]);
  15. }
  16. int x=a[0];
  17. int y=b[0];
  18. for(int i=0; i<n; i++)
  19. {
  20. x=lcm(x,a[i]);
  21. }
  22. for(int j=0; j<m; j++)
  23. {
  24. y=gcd(y,b[j]);
  25. }
  26. int k=1;
  27. int count=0;
  28. int num=x;
  29. while(x<=y)
  30. {
  31. if(y%(x)==0) count++;
  32. k++;
  33. x=num*k;
  34. }
  35. printf("%d",count);
  36. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube