Untitled

 avatar
unknown
plain_text
6 months ago
1.4 kB
2
Indexable
#include <stdio.h>

int n;
int array[200001];

int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a%b);
}

int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        scanf("%d", &array[i]);

    int answer = 0; // The variable to store the answer
    // Why not we just skip the number that we don't want
    // Say i is the index of number we don't want
    for (int i = 0; i < n; i++) {
        int gcd_value; // Used to store the gcd of the sequence
        // Initialization of gcd_value
        // it will be the gcd of first two elements in the 
        // subsequence
        if (i == 0) {
            gcd_value = gcd(array[1], array[2]);
            for (int j = 3; j < n; j++) 
                gcd_value = gcd(gcd_value, array[j]);
        }
        else if (i == 1) {
            gcd_value = gcd(array[0], array[2]);
            for (int j = 3; j < n; j++) 
                gcd_value = gcd(gcd_value, array[j]);
        }
        else {
            // i >= 2
            gcd_value = gcd(array[0], array[1]);
            for (int j = 2; j < n; j++) 
                if (i != j)
                    gcd_value = gcd(gcd_value, array[j]);
        }
        // Here, gcd_value is the gcd of the subsequence of removing a[i]
        if (gcd_value > answer)
            answer = gcd_value;
    }

    printf("%d\n", answer);

    return 0;
}
Editor is loading...
Leave a Comment