Minimum deletions required to make GCD of the array equal to 1
Last Updated :
31 May, 2022
Given an array arr[] of N integers, the task is to find the minimum deletions required to make the GCD of the resulting array elements equal to 1. If it is impossible then print -1.
Examples:
Input: arr[] = {2, 4, 6, 3}
Output: 0
It is clear that GCD(2, 4, 6, 3) = 1
So, we do not need to delete any elements.
Input: arr[] = {8, 14, 16, 26}
Output: -1
No matter how many elements get deleted, the gcd will never be 1.
Approach: If the GCD of the initial array is 1 then we do not need to delete any of the elements and the result will be 0. If GCD is not 1 then the GCD can never be 1 no matter what elements we delete. Let’s say gcd be the gcd of the elements of the array and we delete k elements. Now, N – k elements are left and they still have gcd as their factor. So, it is impossible to get GCD of the array elements equal to 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MinDeletion( int a[], int n)
{
int gcd = 0;
for ( int i = 0; i < n; i++)
gcd = __gcd(gcd, a[i]);
if (gcd > 1)
return -1;
else
return 0;
}
int main()
{
int a[] = { 3, 6, 12, 81, 9 };
int n = sizeof (a) / sizeof (a[0]);
cout << MinDeletion(a, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int __gcd( int a, int b)
{
if (a == 0 )
return b;
if (b == 0 )
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
static int MinDeletion( int a[], int n)
{
int gcd = 0 ;
for ( int i = 0 ; i < n; i++)
gcd = __gcd(gcd, a[i]);
if (gcd > 1 )
return - 1 ;
else
return 0 ;
}
public static void main (String[] args)
{
int a[] = { 3 , 6 , 12 , 81 , 9 };
int n = a.length;
System.out.print(MinDeletion(a, n));
}
}
|
Python3
from math import gcd
def MinDeletion(a, n) :
__gcd = 0 ;
for i in range (n) :
__gcd = gcd(__gcd, a[i]);
if (__gcd > 1 ) :
return - 1 ;
else :
return 0 ;
if __name__ = = "__main__" :
a = [ 3 , 6 , 12 , 81 , 9 ];
n = len (a)
print (MinDeletion(a, n));
|
C#
using System;
class GFG
{
static int __gcd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
static int MinDeletion( int []a, int n)
{
int gcd = 0;
for ( int i = 0; i < n; i++)
gcd = __gcd(gcd, a[i]);
if (gcd > 1)
return -1;
else
return 0;
}
public static void Main ()
{
int []a = { 3, 6, 12, 81, 9 };
int n = a.Length;
Console.WriteLine(MinDeletion(a, n));
}
}
|
Javascript
<script>
function __gcd(a , b) {
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
function MinDeletion(a , n) {
var gcd = 0;
for (i = 0; i < n; i++)
gcd = __gcd(gcd, a[i]);
if (gcd > 1)
return -1;
else
return 0;
}
var a = [ 3, 6, 12, 81, 9 ];
var n = a.length;
document.write(MinDeletion(a, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...