📌  相关文章
📜  删除元素以最大化给定数组的GCD

📅  最后修改于: 2021-05-04 12:27:10             🧑  作者: Mango

给定长度为N≥2的数组arr [] 。任务是从给定的数组中删除一个元素,以使删除后的数组的GCD最大化。

例子:

方法:

  • 想法是找到所有长度(N – 1)子序列的GCD值,并删除该GCD子序列中不存在的元素。找到的最大GCD就是答案。
  • 为了最佳地找到子序列的GCD,请使用单状态动态编程维护一个prefixGCD []和一个suffixGCD []数组。
  • GCD的最大值(前缀GCD [i – 1],后缀GCD [i + 1])是必需的答案。

下面是上述方法的实现:

CPP
// C++ implementation of the above approach
#include 
using namespace std;
  
// Function to return the maximized gcd
// after removing a single element
// from the given array
int MaxGCD(int a[], int n)
{
  
    // Prefix and Suffix arrays
    int Prefix[n + 2];
    int Suffix[n + 2];
  
    // Single state dynamic programming relation
    // for storing gcd of first i elements
    // from the left in Prefix[i]
    Prefix[1] = a[0];
    for (int i = 2; i <= n; i += 1) {
        Prefix[i] = __gcd(Prefix[i - 1], a[i - 1]);
    }
  
    // Initializing Suffix array
    Suffix[n] = a[n - 1];
  
    // Single state dynamic programming relation
    // for storing gcd of all the elements having
    // greater than or equal to i in Suffix[i]
    for (int i = n - 1; i >= 1; i -= 1) {
        Suffix[i] = __gcd(Suffix[i + 1], a[i - 1]);
    }
  
    // If first or last element of
    // the array has to be removed
    int ans = max(Suffix[2], Prefix[n - 1]);
  
    // If any other element is replaced
    for (int i = 2; i < n; i += 1) {
        ans = max(ans, __gcd(Prefix[i - 1], Suffix[i + 1]));
    }
  
    // Return the maximized gcd
    return ans;
}
  
// Driver code
int main()
{
    int a[] = { 14, 17, 28, 70 };
    int n = sizeof(a) / sizeof(a[0]);
  
    cout << MaxGCD(a, n);
  
    return 0;
}


Java
// Java implementation of the above approach 
class Test 
{ 
    // Recursive function to return gcd of a and b 
    static int gcd(int a, int b) 
    { 
        if (b == 0) 
            return a; 
        return gcd(b, a % b); 
    } 
      
    // Function to return the maximized gcd 
    // after removing a single element 
    // from the given array 
    static int MaxGCD(int a[], int n) 
    { 
      
        // Prefix and Suffix arrays 
        int Prefix[] = new int[n + 2]; 
        int Suffix[] = new int[n + 2] ;
      
        // Single state dynamic programming relation 
        // for storing gcd of first i elements 
        // from the left in Prefix[i] 
        Prefix[1] = a[0]; 
        for (int i = 2; i <= n; i += 1)
        { 
            Prefix[i] = gcd(Prefix[i - 1], a[i - 1]); 
        } 
      
        // Initializing Suffix array 
        Suffix[n] = a[n - 1]; 
      
        // Single state dynamic programming relation 
        // for storing gcd of all the elements having 
        // greater than or equal to i in Suffix[i] 
        for (int i = n - 1; i >= 1; i -= 1)
        { 
            Suffix[i] = gcd(Suffix[i + 1], a[i - 1]); 
        } 
      
        // If first or last element of 
        // the array has to be removed 
        int ans = Math.max(Suffix[2], Prefix[n - 1]); 
      
        // If any other element is replaced 
        for (int i = 2; i < n; i += 1) 
        { 
            ans = Math.max(ans, gcd(Prefix[i - 1], Suffix[i + 1])); 
        } 
      
        // Return the maximized gcd 
        return ans; 
    } 
          
    // Driver code 
    public static void main(String[] args) 
    { 
  
        int a[] = { 14, 17, 28, 70 }; 
        int n = a.length; 
      
        System.out.println(MaxGCD(a, n)); 
    } 
} 
  
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the above approach
import math as mt
  
# Function to return the maximized gcd
# after removing a single element
# from the given array
  
def MaxGCD(a, n):
  
  
    # Prefix and Suffix arrays
    Prefix=[0 for i in range(n + 2)]
    Suffix=[0 for i in range(n + 2)]
  
    # Single state dynamic programming relation
    # for storing gcd of first i elements
    # from the left in Prefix[i]
    Prefix[1] = a[0]
    for i in range(2,n+1):
        Prefix[i] = mt.gcd(Prefix[i - 1], a[i - 1])
  
    # Initializing Suffix array
    Suffix[n] = a[n - 1]
  
    # Single state dynamic programming relation
    # for storing gcd of all the elements having
    # greater than or equal to i in Suffix[i]
    for i in range(n-1,0,-1):
        Suffix[i] =mt.gcd(Suffix[i + 1], a[i - 1])
  
    # If first or last element of
    # the array has to be removed
    ans = max(Suffix[2], Prefix[n - 1])
  
    # If any other element is replaced
    for i in range(2,n):
        ans = max(ans, mt.gcd(Prefix[i - 1], Suffix[i + 1]))
  
    # Return the maximized gcd
    return ans
  
# Driver code
  
a=[14, 17, 28, 70]
n = len(a)
  
print(MaxGCD(a, n))
  
# This code is contributed by mohit kumar 29


C#
// C# implementation of the above approach
using System;
  
class GFG
{
      
    // Recursive function to return gcd of a and b 
    static int gcd(int a, int b) 
    { 
        if (b == 0) 
            return a; 
        return gcd(b, a % b); 
    } 
      
    // Function to return the maximized gcd 
    // after removing a single element 
    // from the given array 
    static int MaxGCD(int []a, int n) 
    { 
      
        // Prefix and Suffix arrays 
        int []Prefix = new int[n + 2]; 
        int []Suffix = new int[n + 2] ;
      
        // Single state dynamic programming relation 
        // for storing gcd of first i elements 
        // from the left in Prefix[i] 
        Prefix[1] = a[0]; 
        for (int i = 2; i <= n; i += 1)
        { 
            Prefix[i] = gcd(Prefix[i - 1], a[i - 1]); 
        } 
      
        // Initializing Suffix array 
        Suffix[n] = a[n - 1]; 
      
        // Single state dynamic programming relation 
        // for storing gcd of all the elements having 
        // greater than or equal to i in Suffix[i] 
        for (int i = n - 1; i >= 1; i -= 1)
        { 
            Suffix[i] = gcd(Suffix[i + 1], a[i - 1]); 
        } 
      
        // If first or last element of 
        // the array has to be removed 
        int ans = Math.Max(Suffix[2], Prefix[n - 1]); 
      
        // If any other element is replaced 
        for (int i = 2; i < n; i += 1) 
        { 
            ans = Math.Max(ans, gcd(Prefix[i - 1], Suffix[i + 1])); 
        } 
      
        // Return the maximized gcd 
        return ans; 
    } 
          
    // Driver code 
    static public void Main ()
    {
          
        int []a = { 14, 17, 28, 70 }; 
        int n = a.Length; 
      
        Console.Write(MaxGCD(a, n)); 
    } 
} 
  
// This code is contributed by ajit.


输出:
14

时间复杂度: O(N * log(M))其中M是数组中的最大元素。