📜  删除元素以最小化给定阵列的LCM

📅  最后修改于: 2021-04-28 16:35:32             🧑  作者: Mango

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

例子:

方法:

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

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
  
// Function to return the LCM of two numbers
int lcm(int a, int b)
{
    int GCD = __gcd(a, b);
    return (a * b) / GCD;
}
  
// Function to return the minimum LCM
// after removing a single element
// from the given array
int MinLCM(int a[], int n)
{
  
    // Prefix and Suffix arrays
    int Prefix[n + 2];
    int Suffix[n + 2];
  
    // Single state dynamic programming relation
    // for storing LCM of first i elements
    // from the left in Prefix[i]
    Prefix[1] = a[0];
    for (int i = 2; i <= n; i += 1) {
        Prefix[i] = lcm(Prefix[i - 1], a[i - 1]);
    }
  
    // Initializing Suffix array
    Suffix[n] = a[n - 1];
  
    // Single state dynamic programming relation
    // for storing LCM of all the elements having
    // index greater than or equal to i in Suffix[i]
    for (int i = n - 1; i >= 1; i -= 1) {
        Suffix[i] = lcm(Suffix[i + 1], a[i - 1]);
    }
  
    // If first or last element of
    // the array has to be removed
    int ans = min(Suffix[2], Prefix[n - 1]);
  
    // If any other element is replaced
    for (int i = 2; i < n; i += 1) {
        ans = min(ans, lcm(Prefix[i - 1], Suffix[i + 1]));
    }
  
    // Return the minimum LCM
    return ans;
}
  
// Driver code
int main()
{
    int a[] = { 5, 15, 9, 36 };
    int n = sizeof(a) / sizeof(a[0]);
  
    cout << MinLCM(a, n);
  
    return 0;
}


Java
// Java implementation of the above approach
class GFG
{
  
// Function to return the LCM of two numbers
static int lcm(int a, int b)
{
    int GCD = __gcd(a, b);
    return (a * b) / GCD;
}
  
// Function to return the minimum LCM
// after removing a single element
// from the given array
static int MinLCM(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 LCM of first i elements
    // from the left in Prefix[i]
    Prefix[1] = a[0];
    for (int i = 2; i <= n; i += 1)
    {
        Prefix[i] = lcm(Prefix[i - 1], 
                             a[i - 1]);
    }
  
    // Initializing Suffix array
    Suffix[n] = a[n - 1];
  
    // Single state dynamic programming relation
    // for storing LCM of all the elements having
    // index greater than or equal to i in Suffix[i]
    for (int i = n - 1; i >= 1; i -= 1) 
    {
        Suffix[i] = lcm(Suffix[i + 1],
                             a[i - 1]);
    }
  
    // If first or last element of
    // the array has to be removed
    int ans = Math.min(Suffix[2], 
                       Prefix[n - 1]);
  
    // If any other element is replaced
    for (int i = 2; i < n; i += 1)
    {
        ans = Math.min(ans, lcm(Prefix[i - 1], 
                                Suffix[i + 1]));
    }
  
    // Return the minimum LCM
    return ans;
}
  
static int __gcd(int a, int b) 
{ 
    return b == 0 ? a : __gcd(b, a % b);     
} 
  
// Driver code
public static void main(String []args)
{
    int a[] = { 5, 15, 9, 36 };
    int n = a.length;
  
    System.out.println(MinLCM(a, n));
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of 
# the above approach 
from math import gcd
  
# Function to return the LCM 
# of two numbers 
def lcm(a, b) :
  
    GCD = gcd(a, b); 
    return (a * b) // GCD; 
  
# Function to return the minimum LCM 
# after removing a single element 
# from the given array 
def MinLCM(a, n) :
  
    # Prefix and Suffix arrays 
    Prefix = [0] * (n + 2); 
    Suffix = [0] * (n + 2); 
  
    # Single state dynamic programming relation 
    # for storing LCM of first i elements 
    # from the left in Prefix[i] 
    Prefix[1] = a[0]; 
    for i in range(2, n + 1) :
        Prefix[i] = lcm(Prefix[i - 1], 
                             a[i - 1]); 
  
    # Initializing Suffix array 
    Suffix[n] = a[n - 1]; 
  
    # Single state dynamic programming relation 
    # for storing LCM of all the elements having 
    # index greater than or equal to i in Suffix[i] 
    for i in range(n - 1, 0, -1) :
        Suffix[i] = lcm(Suffix[i + 1], a[i - 1]); 
      
    # If first or last element of 
    # the array has to be removed 
    ans = min(Suffix[2], Prefix[n - 1]); 
  
    # If any other element is replaced 
    for i in range(2, n) :
        ans = min(ans, lcm(Prefix[i - 1], 
                           Suffix[i + 1])); 
      
    # Return the minimum LCM 
    return ans; 
  
# Driver code 
if __name__ == "__main__" : 
  
    a = [ 5, 15, 9, 36 ]; 
    n = len(a); 
  
    print(MinLCM(a, n));
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the above approach
using System;
      
class GFG
{
  
// Function to return the LCM of two numbers
static int lcm(int a, int b)
{
    int GCD = __gcd(a, b);
    return (a * b) / GCD;
}
  
// Function to return the minimum LCM
// after removing a single element
// from the given array
static int MinLCM(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 LCM of first i elements
    // from the left in Prefix[i]
    Prefix[1] = a[0];
    for (int i = 2; i <= n; i += 1)
    {
        Prefix[i] = lcm(Prefix[i - 1], 
                             a[i - 1]);
    }
  
    // Initializing Suffix array
    Suffix[n] = a[n - 1];
  
    // Single state dynamic programming relation
    // for storing LCM of all the elements having
    // index greater than or equal to i in Suffix[i]
    for (int i = n - 1; i >= 1; i -= 1) 
    {
        Suffix[i] = lcm(Suffix[i + 1],
                             a[i - 1]);
    }
  
    // If first or last element of
    // the array has to be removed
    int ans = Math.Min(Suffix[2], 
                       Prefix[n - 1]);
  
    // If any other element is replaced
    for (int i = 2; i < n; i += 1)
    {
        ans = Math.Min(ans, lcm(Prefix[i - 1], 
                                Suffix[i + 1]));
    }
  
    // Return the minimum LCM
    return ans;
}
  
static int __gcd(int a, int b) 
{ 
    return b == 0 ? a : __gcd(b, a % b);     
} 
  
// Driver code
public static void Main(String []args)
{
    int []a = { 5, 15, 9, 36 };
    int n = a.Length;
  
    Console.WriteLine(MinLCM(a, n));
}
}
  
// This code is contributed by PrinciRaj1992


输出:
45

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