C Program to Find the maximum GCD after replacing the array with almost one element

Write a program to find the maximum GCD after replacing the array with almost one element.

For example :

The input is: 4, 5, 8
The output is 4 

Explanation :

Replace 5 with 12 we will get 4 as the maximum GCD

Input Format :
Input has the size and the elements

Constraint:
1 <= N <= 1000

Output Format:
Print the GCDSample Input 1

3
12 18 10

Sample Output 1

6

Code:

#include<stdio.h>


int main()
{
    int n,a[100];
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    int max =a[0];
    for(int i=1;i<n;i++)
    {
        if(a[i]>max)
            max=a[i];

    }
    int gcd=1;
    int f=0;
    for(int i =2;i<max;i++)
    {
        f=0;
        int c=0;
        for(int j=0;j<n;j++)
        {
            if(a[j] % i !=0)
            {
                c++;
                
            }
            if( c == 2)
            {
                f=1;
                 break;}
        }
        if(f == 0) gcd =i;

    }
    printf("%d",gcd);

}
Code language: C/AL (cal)

Leave a Comment