Advertisement
porosh45

21-9-18 C.cpp

Sep 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int gcd(int a,int b)
  4. {
  5. if(a==0)
  6. return b;
  7. return gcd(b%a,a);
  8. }
  9. int find_gcd(int a[],int n,int start)
  10. {
  11. int result = a[start];
  12. for(int i = start ;i<n;i++)
  13. result = gcd(a[start],a[i]);
  14.  
  15. return result;
  16. }
  17. int main()
  18. {
  19. int n;
  20. cin>>n;
  21. int a[n],result[n];
  22. for(int i = 0;i<n;i++)
  23. cin>>a[i];
  24. sort(a,a+n);
  25. for(int i = 0; i < n - 1 ; i++)
  26. {
  27. result[i]=find_gcd(a,n,i);
  28. // cout<<result[i]<<endl;
  29. }
  30. int res = 0, k,c = 0;
  31. for(int i = 0 ;i< n-1;i++)
  32. {
  33. if(result[i]>result[0])
  34. {
  35. res = result[i];
  36. k=i;
  37. c=1;
  38. }
  39. }
  40. if(c)
  41. cout<<k<<endl;
  42. else
  43. cout<<"-1"<<endl;
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement