Posts Delete one
Post
Cancel

Delete one

PROBLEM DESCRIPTION

Given an integer array A of size N. You have to delete one element such that the GCD(Greatest common divisor) of the remaining array is maximum. Find the maximum value of GCD.

geeksforgeeks

SOLUTION

GCD Theory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class Solution {
    
    public int solve(int[] A) {

        int n = A.length;

        //prefix array from L-R using GCD
        int[] pfl = new int[n];
        //suffix array from R-L using GCD
        int[] pfr = new int[n];

        //init
        pfl[0] = A[0];
        pfr[n-1] = A[n-1];

        for(int i=1; i<n; i++){
            pfl[i] = gcd(pfl[i-1], A[i]);
        }

        for(int i=n-2; i>=0; i--){
            pfr[i] = gcd(pfr[i+1], A[i]);
        }

        //ans
        int max = 1;

        //If we remove element at index i, the GCD for remaining elements will be:
        //GCD of (GCD of elements from [0, i-1], GCD of elements from [i+1, n-1])
        //= GCD of (pfl[i-1], pfr[i+1])
        for(int i=1; i<=n-2; i++){
            max = Math.max(max, gcd(pfl[i-1], pfr[i+1]));
        }

        //If we remove the first element, GCD = pfr[1]
        max = Math.max(max, pfr[1]);

        //If we remove the last element, GCD = pfl[n-2]
        max = Math.max(max, pfl[n-2]);
        
        return max;

    }

    //gcd of a and b
    public int gcd(int a, int b){
        if(b == 0) return a;
        return gcd(b, a%b);
    }

}
This post is licensed under CC BY 4.0 by the author.