Sunday, October 6, 2024
Google search engine
HomeData Modelling & AIRemove an element to minimize the LCM of the given array

Remove an element to minimize the LCM of the given array

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


Javascript




<script>
 
// JavaScript implementation of the above approach
 
 
// Function to return the LCM of two numbers
function lcm(a, b) {
    let GCD = __gcd(a, b);
    return Math.floor((a * b) / GCD);
}
 
function __gcd(a, b)
{
    return b == 0 ? a : __gcd(b, a % b);    
}
 
// Function to return the minimum LCM
// after removing a single element
// from the given array
function MinLCM(a, n) {
 
    // Prefix and Suffix arrays
    let Prefix = new Array(n + 2);
    let Suffix = new Array(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 (let 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 (let 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
    let ans = Math.min(Suffix[2], Prefix[n - 1]);
 
    // If any other element is replaced
    for (let i = 2; i < n; i += 1) {
        ans = Math.min(ans, lcm(Prefix[i - 1], Suffix[i + 1]));
    }
 
    // Return the minimum LCM
    return ans;
}
 
// Driver code
 
let a = [5, 15, 9, 36];
let n = a.length;
 
document.write(MinLCM(a, n));
 
</script>


Output: 

45

 

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

Auxiliary Space: O(N)

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments