Given an array arr[] of length N ≥ 2. The task is to remove an element from the given array such that the GCD of the array after removing it is maximized.
Examples:
Input: arr[] = {12, 15, 18}
Output: 6
Remove 12: GCD(15, 18) = 3
Remove 15: GCD(12, 18) = 6
Remove 18: GCD(12, 15) = 3
Input: arr[] = {14, 17, 28, 70}
Output: 14
Approach:
In this approach, we iterate through each element of the array and remove it to get the remaining array. We then calculate the GCD of the remaining array and keep track of the maximum GCD found so far. Finally, we return the maximum GCD.
- Define a function gcd to calculate the greatest common divisor of two integers using the Euclidean algorithm.
- Define a function MaxGCD which takes an array a and its length n as input and returns the maximum possible GCD after removing one element from the array.
- Initialize a variable ans to 0.
- Iterate over the array and remove each element in turn. To do this, create a temporary array temp of size n-1, and copy all elements from a except the current element to temp.
- Calculate the GCD of the elements in temp using the gcd function. Set this value to a variable res.
- Update the value of ans to be the maximum of its current value and res.
- Return the value of ans as the final answer.
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b) {
if (b == 0) return a;
return gcd(b, a%b);
}
int MaxGCD( int a[], int n) {
int ans = 0;
for ( int i = 0; i < n; i++) {
int temp[n-1];
int k = 0;
for ( int j = 0; j < n; j++) {
if (j != i) {
temp[k++] = a[j];
}
}
int res = temp[0];
for ( int j = 1; j < n-1; j++) {
res = gcd(res, temp[j]);
}
ans = max(ans, res);
}
return ans;
}
int main() {
int a[] = {14, 17, 28, 70};
int n = sizeof (a)/ sizeof (a[0]);
cout << MaxGCD(a, n) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
int [] a = { 14 , 17 , 28 , 70 };
int n = a.length;
System.out.println(MaxGCD(a, n));
}
public static int gcd( int a, int b) {
if (b == 0 )
return a;
return gcd(b, a % b);
}
public static int MaxGCD( int [] a, int n) {
int ans = 0 ;
for ( int i = 0 ; i < n; i++) {
int [] temp = new int [n - 1 ];
int k = 0 ;
for ( int j = 0 ; j < n; j++) {
if (j != i) {
temp[k++] = a[j];
}
}
int res = temp[ 0 ];
for ( int j = 1 ; j < n - 1 ; j++) {
res = gcd(res, temp[j]);
}
ans = Math.max(ans, res);
}
return ans;
}
}
|
Python3
def gcd(a, b):
if b = = 0 :
return a
return gcd(b, a % b)
def max_gcd(arr):
n = len (arr)
ans = 0
for i in range (n):
temp = arr[:i] + arr[i + 1 :]
res = temp[ 0 ]
for j in range ( 1 , n - 1 ):
res = gcd(res, temp[j])
ans = max (ans, res)
return ans
if __name__ = = "__main__" :
a = [ 14 , 17 , 28 , 70 ]
print (max_gcd(a))
|
C#
using System;
namespace MaxGCD
{
class Program
{
static int Gcd( int a, int b)
{
if (b == 0)
return a;
return Gcd(b, a % b);
}
static int MaxGCD( int [] a, int n)
{
int ans = 0;
for ( int i = 0; i < n; i++)
{
int [] temp = new int [n - 1];
int k = 0;
for ( int j = 0; j < n; j++)
{
if (j != i)
{
temp[k++] = a[j];
}
}
int res = temp[0];
for ( int j = 1; j < n - 1; j++)
{
res = Gcd(res, temp[j]);
}
ans = Math.Max(ans, res);
}
return ans;
}
static void Main( string [] args)
{
int [] a = { 14, 17, 28, 70 };
int n = a.Length;
Console.WriteLine(MaxGCD(a, n));
}
}
}
|
Time Complexity: O(n^2 * log(max(a))), where n is the size of the array and max(a) is the maximum value in the array. This is because the function gcd has a time complexity of O(log(max(a))) and the function MaxGCD has two nested loops, each of which iterates n times.
Auxiliary Space: O(n^2) because the function MaxGCD creates a new array of size n-1 for each element of the input array, resulting in n^2 total elements in all created arrays.
Approach:
- Idea is to find the GCD value of all the sub-sequences of length (N – 1) and removing the element which is not present in the sub-sequence with that GCD. The maximum GCD found would be the answer.
- To find the GCD of the sub-sequences optimally, maintain a prefixGCD[] and a suffixGCD[] array using single state dynamic programming.
- The maximum value of GCD(prefixGCD[i – 1], suffixGCD[i + 1]) is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MaxGCD( int a[], int n)
{
int Prefix[n + 2];
int Suffix[n + 2];
Prefix[1] = a[0];
for ( int i = 2; i <= n; i += 1) {
Prefix[i] = __gcd(Prefix[i - 1], a[i - 1]);
}
Suffix[n] = a[n - 1];
for ( int i = n - 1; i >= 1; i -= 1) {
Suffix[i] = __gcd(Suffix[i + 1], a[i - 1]);
}
int ans = max(Suffix[2], Prefix[n - 1]);
for ( int i = 2; i < n; i += 1) {
ans = max(ans, __gcd(Prefix[i - 1], Suffix[i + 1]));
}
return ans;
}
int main()
{
int a[] = { 14, 17, 28, 70 };
int n = sizeof (a) / sizeof (a[0]);
cout << MaxGCD(a, n);
return 0;
}
|
Java
class Test
{
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int MaxGCD( int a[], int n)
{
int Prefix[] = new int [n + 2 ];
int Suffix[] = new int [n + 2 ] ;
Prefix[ 1 ] = a[ 0 ];
for ( int i = 2 ; i <= n; i += 1 )
{
Prefix[i] = gcd(Prefix[i - 1 ], a[i - 1 ]);
}
Suffix[n] = a[n - 1 ];
for ( int i = n - 1 ; i >= 1 ; i -= 1 )
{
Suffix[i] = gcd(Suffix[i + 1 ], a[i - 1 ]);
}
int ans = Math.max(Suffix[ 2 ], Prefix[n - 1 ]);
for ( int i = 2 ; i < n; i += 1 )
{
ans = Math.max(ans, gcd(Prefix[i - 1 ], Suffix[i + 1 ]));
}
return ans;
}
public static void main(String[] args)
{
int a[] = { 14 , 17 , 28 , 70 };
int n = a.length;
System.out.println(MaxGCD(a, n));
}
}
|
Python3
import math as mt
def MaxGCD(a, n):
Prefix = [ 0 for i in range (n + 2 )]
Suffix = [ 0 for i in range (n + 2 )]
Prefix[ 1 ] = a[ 0 ]
for i in range ( 2 ,n + 1 ):
Prefix[i] = mt.gcd(Prefix[i - 1 ], a[i - 1 ])
Suffix[n] = a[n - 1 ]
for i in range (n - 1 , 0 , - 1 ):
Suffix[i] = mt.gcd(Suffix[i + 1 ], a[i - 1 ])
ans = max (Suffix[ 2 ], Prefix[n - 1 ])
for i in range ( 2 ,n):
ans = max (ans, mt.gcd(Prefix[i - 1 ], Suffix[i + 1 ]))
return ans
a = [ 14 , 17 , 28 , 70 ]
n = len (a)
print (MaxGCD(a, n))
|
C#
using System;
class GFG
{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int MaxGCD( int []a, int n)
{
int []Prefix = new int [n + 2];
int []Suffix = new int [n + 2] ;
Prefix[1] = a[0];
for ( int i = 2; i <= n; i += 1)
{
Prefix[i] = gcd(Prefix[i - 1], a[i - 1]);
}
Suffix[n] = a[n - 1];
for ( int i = n - 1; i >= 1; i -= 1)
{
Suffix[i] = gcd(Suffix[i + 1], a[i - 1]);
}
int ans = Math.Max(Suffix[2], Prefix[n - 1]);
for ( int i = 2; i < n; i += 1)
{
ans = Math.Max(ans, gcd(Prefix[i - 1], Suffix[i + 1]));
}
return ans;
}
static public void Main ()
{
int []a = { 14, 17, 28, 70 };
int n = a.Length;
Console.Write(MaxGCD(a, n));
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function MaxGCD(a, n)
{
let Prefix = new Array(n + 2);
let Suffix = new Array(n + 2);
Prefix[1] = a[0];
for (let i = 2; i <= n; i += 1)
{
Prefix[i] = gcd(Prefix[i - 1], a[i - 1]);
}
Suffix[n] = a[n - 1];
for (let i = n - 1; i >= 1; i -= 1)
{
Suffix[i] = gcd(Suffix[i + 1], a[i - 1]);
}
let ans = Math.max(Suffix[2], Prefix[n - 1]);
for (let i = 2; i < n; i += 1)
{
ans = Math.max(ans, gcd(Prefix[i - 1],
Suffix[i + 1]));
}
return ans;
}
let a = [ 14, 17, 28, 70 ];
let n = a.length;
document.write(MaxGCD(a, n));
</script>
|
Time Complexity: O(N * log(M)) where M is the maximum element from the array.
Auxiliary Space: O(N)