Minimum deletions required to make GCD of the array equal to 1

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the minimum
// deletions required
int MinDeletion(int a[], int n)
{
  
    // To store the GCD of the array
    int gcd = 0;
    for (int i = 0; i < n; i++)
        gcd = __gcd(gcd, a[i]);
  
    // GCD cannot be 1
    if (gcd > 1)
        return -1;
  
    // GCD of the elements is already 1
    else
        return 0;
}
  
// Driver code
int main()
{
    int a[] = { 3, 6, 12, 81, 9 };
    int n = sizeof(a) / sizeof(a[0]);
  
    cout << MinDeletion(a, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.io.*;
  
class GFG
{
  
    // Recursive function to return gcd of a and b 
    static int __gcd(int a, int b) 
    
        // Everything divides 0 
        if (a == 0
        return b; 
        if (b == 0
        return a; 
          
        // base case 
        if (a == b) 
            return a; 
          
        // a is greater 
        if (a > b) 
            return __gcd(a-b, b); 
        return __gcd(a, b-a); 
    
  
    // Function to return the minimum
    // deletions required
    static int MinDeletion(int a[], int n)
    {
      
        // To store the GCD of the array
        int gcd = 0;
        for (int i = 0; i < n; i++)
            gcd = __gcd(gcd, a[i]);
      
        // GCD cannot be 1
        if (gcd > 1)
            return -1;
      
        // GCD of the elements is already 1
        else
            return 0;
    }
  
  
    // Driver code
    public static void main (String[] args) 
    {
        int a[] = { 3, 6, 12, 81, 9 };
        int n = a.length;
  
        System.out.print(MinDeletion(a, n));
    }
}
  
// This code is contributed by anuj_67..

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
from math import gcd
  
# Function to return the minimum 
# deletions required 
def MinDeletion(a, n) : 
  
    # To store the GCD of the array 
    __gcd = 0
    for i in range(n) :
        __gcd = gcd(__gcd, a[i]); 
  
    # GCD cannot be 1 
    if (__gcd > 1) :
        return -1
  
    # GCD of the elements is already 1 
    else :
        return 0
  
# Driver code 
if __name__ == "__main__" :
  
    a = [ 3, 6, 12, 81, 9 ]; 
    n = len(a) 
  
    print(MinDeletion(a, n)); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
  
    // Recursive function to return gcd of a and b 
    static int __gcd(int a, int b) 
    
        // Everything divides 0 
        if (a == 0) 
            return b; 
        if (b == 0) 
            return a; 
          
        // base case 
        if (a == b) 
            return a; 
          
        // a is greater 
        if (a > b) 
            return __gcd(a-b, b); 
        return __gcd(a, b-a); 
    
  
    // Function to return the minimum
    // deletions required
    static int MinDeletion(int []a, int n)
    {
      
        // To store the GCD of the array
        int gcd = 0;
        for (int i = 0; i < n; i++)
            gcd = __gcd(gcd, a[i]);
      
        // GCD cannot be 1
        if (gcd > 1)
            return -1;
      
        // GCD of the elements is already 1
        else
            return 0;
    }
  
  
    // Driver code
    public static void Main () 
    {
        int []a = { 3, 6, 12, 81, 9 };
        int n = a.Length;
  
        Console.WriteLine(MinDeletion(a, n));
    }
}
  
// This code is contributed by anuj_67..

chevron_right


Output:

-1

Time Complexity: O(N)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, vt_m