Skip to content
Related Articles

Related Articles

Remove an element to minimize the LCM of the given array
  • Last Updated : 20 Nov, 2019

Given an array arr[] of length N ≥ 2. The task is to remove an element from the given array such that the LCM of the array after removing it is minimized.

Examples:

Input: arr[] = {18, 12, 24}
Output: 24
Remove 12: LCM(18, 24) = 72
Remove 18: LCM(12, 24) = 24
Remove 24: LCM(12, 18) = 36

Input: arr[] = {5, 15, 9, 36}
Output: 45

Approach:



  • Idea is to find the LCM value of all the sub-sequences of length (N – 1) and removing the element which is not present in the sub-sequence with that LCM. The minimum LCM found would be the answer.
  • To find the LCM of the sub-sequences optimally, maintain a prefixLCM[] and a suffixLCM[] array using single state dynamic programming.
  • The minimum value of LCM(prefixLCM[i – 1], suffixLCM[i + 1]) is the required answer.

Below is the implementation of the above approach:

C++




// C++ implementation of the above approach
#include <bits/stdc++.h>
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
Output:
45

Time Complexity: O(N * log(M)) where M is the maximum element from the array.

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :