Remove an element to minimize the LCM of the given array

• Last Updated : 03 Jun, 2021

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 using namespace std; // Function to return the LCM of two numbersint 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 arrayint 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 = a;    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, 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 codeint main(){    int a[] = { 5, 15, 9, 36 };    int n = sizeof(a) / sizeof(a);     cout << MinLCM(a, n);     return 0;}

Java

 // Java implementation of the above approachclass GFG{ // Function to return the LCM of two numbersstatic 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 arraystatic 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 = a;    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,                       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 codepublic 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 approachfrom math import gcd # Function to return the LCM# of two numbersdef 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 arraydef MinLCM(a, n) :     # Prefix and Suffix arrays    Prefix =  * (n + 2);    Suffix =  * (n + 2);     # Single state dynamic programming relation    # for storing LCM of first i elements    # from the left in Prefix[i]    Prefix = a;    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, 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 codeif __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 approachusing System;     class GFG{ // Function to return the LCM of two numbersstatic 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 arraystatic 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 = a;    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,                       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 codepublic 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

Javascript


Output:
45

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

My Personal Notes arrow_drop_up