Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- // Recursive function to return gcd of a and b
- int GCD(int a, int b){
- return b == 0 ? a : GCD(b, a % b);
- }
- int main(){
- long long n, k;
- cin >> n >> k;
- vector <long long> a(n);
- vector <long long> prefixGCD(n);
- for (long long i = 0; i < n; i++) {
- cin >> a[i];
- }
- long long gcd_max = a[0];
- long long start = 0, end = 0;
- //______________O(kN)_________________//
- for (long long i = 0; i <= n-k; i++) {
- long long gcd = 0;
- for (long long j = i; j < i+k ; ++j){
- gcd = GCD(gcd, a[j]);
- printf("%3lld -> gcd %3lld; ",a[j],gcd);
- }
- if (gcd > gcd_max){
- gcd_max = gcd;
- start = i;
- end = i+k;
- }
- cout<<" --> "<<gcd_max<<endl;
- }
- cout << gcd_max <<endl;
- cout << start << " " << end << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment