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

📅  最后修改于: 2021-10-26 05:52:31             🧑  作者: 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..


Javascript


输出:

-1

时间复杂度: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程