Given an array arr[] of size N, the task is to find the minimum LCM (Least Common Multiple) of all unique pairs in the given array, where 1 <= N <= 105, 1 <= arr[i] <= 105.
Examples:
Input: arr[] = {2, 4, 3}
Output: 4
Expalanation
LCM (2, 4) = 4
LCM (2, 3) = 6
LCM (4, 3) = 12
Minimum possible LCM is 4.Input: arr [] ={1, 5, 2, 2, 6}
Output: 2
Naive Approach
- Generate all possible pairs and compute LCM for every unique pairs.
- Find the minimum LCM from all unique pairs.
Below is the implementation of the above approach:
C++
// C++ program to find // minimum possible lcm // from any pair #include <bits/stdc++.h> using namespace std; // function to compute // GCD of two numbers int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } // function that return // minimum possible lcm // from any pair int minLCM(int arr[], int n) { int ans = INT_MAX; for (int i = 0; i < n; i++) { // fix the ith element and // iterate over all the array // to find minimum LCM for (int j = i + 1; j < n; j++) { int g = gcd(arr[i], arr[j]); int lcm = arr[i] / g * arr[j]; ans = min(ans, lcm); } } return ans; } // Driver code int main() { int arr[] = { 2, 4, 3, 6, 5 }; int n = sizeof(arr) / sizeof(arr[0]); cout << minLCM(arr, n) << endl; return 0; } |
Java
// Java program to find minimum // possible lcm from any pair import java.io.*; import java.util.*; class GFG { // Function to compute // GCD of two numbers static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } // Function that return minimum // possible lcm from any pair static int minLCM(int arr[], int n) { int ans = Integer.MAX_VALUE; for(int i = 0; i < n; i++) { // Fix the ith element and // iterate over all the array // to find minimum LCM for(int j = i + 1; j < n; j++) { int g = gcd(arr[i], arr[j]); int lcm = arr[i] / g * arr[j]; ans = Math.min(ans, lcm); } } return ans; } // Driver code public static void main(String[] args) { int arr[] = { 2, 4, 3, 6, 5 }; int n = arr.length; System.out.println(minLCM(arr,n)); } } // This code is contributed by coder001 |
Python3
# Python3 program to find minimum # possible lcm from any pair import sys # Function to compute # GCD of two numbers def gcd(a, b): if (b == 0): return a; return gcd(b, a % b); # Function that return minimum # possible lcm from any pair def minLCM(arr, n): ans = 1000000000; for i in range(n): # Fix the ith element and # iterate over all the # array to find minimum LCM for j in range(i + 1, n): g = gcd(arr[i], arr[j]); lcm = arr[i] / g * arr[j]; ans = min(ans, lcm); return ans; # Driver code arr = [ 2, 4, 3, 6, 5 ]; print(minLCM(arr, 5)) # This code is contributed by grand_master |
C#
// C# program to find minimum // possible lcm from any pair using System; class GFG{ // Function to compute // GCD of two numbers static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } // Function that return minimum // possible lcm from any pair static int minLCM(int []arr, int n) { int ans = Int32.MaxValue; for(int i = 0; i < n; i++) { // Fix the ith element and // iterate over all the array // to find minimum LCM for(int j = i + 1; j < n; j++) { int g = gcd(arr[i], arr[j]); int lcm = arr[i] / g * arr[j]; ans = Math.Min(ans, lcm); } } return ans; } // Driver code public static void Main() { int []arr = { 2, 4, 3, 6, 5 }; int n = arr.Length; Console.Write(minLCM(arr,n)); } } // This code is contributed by Akanksha_Rai |
4
Time Complexity: O(N2)
Efficient Approach: This approach depends upon the formula:
Product of two number = LCM of two number * GCD of two number
- In the formula of LCM, the denominator is the GCD of two numbers, and GCD of two number will never be greater than the number itself.
- So for a fixed GCD, find the smallest two multiples of that fixed GCD that is present in the given array.
- Store only the smallest two multiples of each GCD because choosing a bigger multiple of GCD that is present in the array, no matter what, it will never give the minimum answer.
- Finally use sieve to find the minimum two number that is the multiple of the chosen GCD.
Below is the implementation of above approach:
C++
// C++ program to find the // pair having minimum LCM #include <bits/stdc++.h> using namespace std; // function that return // pair having minimum LCM int minLCM(int arr[], int n) { int mx = 0; for (int i = 0; i < n; i++) { // find max element in the array as // the gcd of two elements from the // array can't greater than max element. mx = max(mx, arr[i]); } // created a 2D array to store minimum // two multiple of any particular i. vector<vector<int> > mul(mx + 1); for (int i = 0; i < n; i++) { if (mul[arr[i]].size() > 1) { // we already found two // smallest multiple continue; } mul[arr[i]].push_back(arr[i]); } // iterating over all gcd for (int i = 1; i <= mx; i++) { // iterating over its multiple for (int j = i + i; j <= mx; j += i) { if (mul[i].size() > 1) { // if we already found the // two smallest multiple of i break; } for (int k : mul[j]) { if (mul[i].size() > 1) break; mul[i].push_back(k); } } } int ans = INT_MAX; for (int i = 1; i <= mx; i++) { if (mul[i].size() <= 1) continue; // choosing smallest two multiple int a = mul[i][0], b = mul[i][1]; // calculating lcm int lcm = (a * b) / i; ans = min(ans, lcm); } // return final answer return ans; } // Driver code int main() { int arr[] = { 2, 4, 3, 6, 5 }; int n = sizeof(arr) / sizeof(arr[0]); cout << minLCM(arr, n) << endl; return 0; } |
4
Time Complexity: O((N + M) * log(M))
Auxiliary Space: O(M) where M is the maximum element in the array
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum sum by choosing minimum of pairs from array
- Find minimum GCD of all pairs in an array
- Minimum sum of product of elements of pairs of the given array
- Find the minimum value of the given expression over all pairs of the array
- Find k ordered pairs in array with minimum difference d
- Count of all pairs in an Array with minimum absolute difference
- Minimum value of maximum absolute difference of all adjacent pairs in an Array
- Minimum cost to make array size 1 by removing larger of pairs
- Ways to form n/2 pairs such that difference of pairs is minimum
- Find the maximum cost of an array of pairs choosing at most K pairs
- Minimum and Maximum sum of absolute differences of pairs
- Minimum sum of absolute difference of pairs of two arrays
- Minimum sum obtained by choosing N number from given N pairs
- Minimum and Maximum number of pairs in m teams of n people
- Maximum of minimum difference of all pairs from subsequences of given size
- Find pairs in array whose sums already exist in array
- Minimum number of swaps required for arranging pairs adjacent to each other
- Find pairs in array whose sum does not exist in Array
- Maximum and minimum of an array using minimum number of comparisons
- Minimum distance between the maximum and minimum element of a given Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

