📌  相关文章
📜  使数组的GCD等于1所需的最小删除

📅  最后修改于: 2021-04-24 19:22:54             🧑  作者: Mango

给定N个整数的数组arr [] ,任务是找到使所得数组元素的GCD等于1所需的最小删除。如果不可能,则打印-1

例子:

方法:如果初始数组的GCD为1,那么我们不需要删除任何元素,结果将为0。如果GCD不为1,则无论我们删除什么元素,GCD都永远不会为1。假设gcd是数组元素的gcd,我们删除了k个元素。现在,剩下N – k个元素,并且它们仍然具有gcd作为其因子。因此,不可能获得等于1的数组元素的GCD。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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;
}


Java
// 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..


Python3
# 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


C#
// 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..


输出:
-1

时间复杂度: O(N)