Given an array arr[], the task is to find the maximum LCM when the elements of the array are taken in pairs.
Examples:
Input: arr[] = {17, 3, 8, 6}
Output: 136
Explanation:
Respective Pairs with their LCM are:
{8, 17} has LCM 136,
{3, 17} has LCM 51,
{6, 17} has LCM 102,
{3, 8} has LCM 24,
{3, 6} has LCM 6, and
{6, 8} has LCM 24.
Maximum LCM among these =136.
Input: array[] = {1, 8, 12, 9}
Output: 72
Explanation:
72 is the highest LCM among all the pairs of the given array.
Naive Approach: Use two loops to generate all possible pairs of elements of the array and calculate LCM of them. Update the LCM whenever we get a higher value.
Time Complexity: O(N2)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxLcmOfPairs( int arr[], int n)
{
int maxLCM = -1;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
maxLCM
= max(maxLCM, (arr[i] * arr[j])
/ __gcd(arr[i], arr[j]));
}
}
return maxLCM;
}
int main()
{
int arr[] = { 17, 3, 8, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << maxLcmOfPairs(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int maxLcmOfPairs( int arr[], int n)
{
int maxLCM = - 1 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = i + 1 ; j < n; j++) {
maxLCM = Math.max(
maxLCM, (arr[i] * arr[j])
/ __gcd(arr[i], arr[j]));
}
}
return maxLCM;
}
static int __gcd( int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
public static void main(String[] args)
{
int arr[] = { 17 , 3 , 8 , 6 };
int n = arr.length;
System.out.print(maxLcmOfPairs(arr, n));
}
}
|
Python3
from math import gcd
def maxLcmOfPairs(arr, n):
maxLCM = - 1
for i in range (n):
for j in range (i + 1 , n, 1 ):
maxLCM = max (maxLCM, (arr[i] * arr[j]) / /
gcd(arr[i], arr[j]))
return maxLCM
if __name__ = = '__main__' :
arr = [ 17 , 3 , 8 , 6 ]
n = len (arr)
print (maxLcmOfPairs(arr, n))
|
C#
using System;
class GFG {
static int maxLcmOfPairs( int [] arr, int n)
{
int maxLCM = -1;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
maxLCM = Math.Max(
maxLCM, (arr[i] * arr[j])
/ __gcd(arr[i], arr[j]));
}
}
return maxLCM;
}
static int __gcd( int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
public static void Main()
{
int [] arr = { 17, 3, 8, 6 };
int n = arr.Length;
Console.Write(maxLcmOfPairs(arr, n));
}
}
|
Javascript
<script>
function maxLcmOfPairs(arr , n)
{
var maxLCM = -1;
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
maxLCM = Math.max(maxLCM, (arr[i] * arr[j]) / __gcd(arr[i], arr[j]));
}
}
return maxLCM;
}
function __gcd(a , b) {
return b == 0 ? a : __gcd(b, a % b);
}
var arr = [ 17, 3, 8, 6 ];
var n = arr.length;
document.write(maxLcmOfPairs(arr, n));
</script>
|
Another Approach:
We can use Greedy Method. For applying the greedy approach we have to sort the given array and then comparing LCM of pairs of elements of the array and finally compute the maximum value of LCM.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int greedyLCM( int arr[], int n)
{
sort(arr, arr + n);
int maxLCM = arr[n - 1];
for ( int i = n - 1; i >= 0; i--) {
if (arr[i] * arr[i] < maxLCM)
break ;
for ( int j = i - 1; j >= 0; j--) {
if (arr[i] * arr[j] < maxLCM)
break ;
else
maxLCM = max(maxLCM,
(arr[i] * arr[j])
/ __gcd(arr[i], arr[j]));
}
}
return maxLCM;
}
int main()
{
int arr[] = { 17, 3, 8, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << greedyLCM(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int greedyLCM( int arr[], int n)
{
Arrays.sort(arr);
int maxLCM = arr[n - 1 ];
for ( int i = n - 1 ; i >= 0 ; i--) {
if (arr[i] * arr[i] < maxLCM)
break ;
for ( int j = i - 1 ; j >= 0 ; j--) {
if (arr[i] * arr[j] < maxLCM)
break ;
else
maxLCM = Math.max(
maxLCM,
(arr[i] * arr[j])
/ __gcd(arr[i], arr[j]));
}
}
return maxLCM;
}
static int __gcd( int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
public static void main(String[] args)
{
int arr[] = { 17 , 3 , 8 , 6 };
int n = arr.length;
System.out.print(greedyLCM(arr, n));
}
}
|
Python3
from math import gcd
def greedyLCM(arr, n):
arr.sort()
maxLCM = arr[n - 1 ]
for i in range (n - 1 , - 1 , - 1 ):
if (arr[i] * arr[i] < maxLCM):
break
for j in range (i - 1 , - 1 , - 1 ):
if (arr[i] * arr[j] < maxLCM):
break
else :
maxLCM = max (maxLCM,
(arr[i] * arr[j]) / /
gcd(arr[i], arr[j]))
return maxLCM
arr = [ 17 , 3 , 8 , 6 ]
n = len (arr)
print (greedyLCM(arr, n))
|
C#
using System;
class GFG {
static int greedyLCM( int [] arr, int n)
{
Array.Sort(arr);
int maxLCM = arr[n - 1];
for ( int i = n - 1; i >= 0; i--) {
if (arr[i] * arr[i] < maxLCM)
break ;
for ( int j = i - 1; j >= 0; j--) {
if (arr[i] * arr[j] < maxLCM)
break ;
else
maxLCM = Math.Max(
maxLCM,
(arr[i] * arr[j])
/ __gcd(arr[i], arr[j]));
}
}
return maxLCM;
}
static int __gcd( int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
public static void Main(String[] args)
{
int [] arr = { 17, 3, 8, 6 };
int n = arr.Length;
Console.Write(greedyLCM(arr, n));
}
}
|
Javascript
<script>
function greedyLCM(arr, n)
{
arr.sort( function (a, b){ return a - b});
let maxLCM = arr[n - 1];
for (let i = n - 1; i >= 0; i--)
{
if (arr[i] * arr[i] < maxLCM)
break ;
for (let j = i - 1; j >= 0; j--)
{
if (arr[i] * arr[j] < maxLCM)
break ;
else
maxLCM = Math.max(maxLCM,
parseInt((arr[i] * arr[j]) /
__gcd(arr[i], arr[j]), 10));
}
}
return maxLCM;
}
function __gcd(a, b)
{
return b == 0 ? a : __gcd(b, a % b);
}
let arr = [ 17, 3, 8, 6 ];
let n = arr.length;
document.write(greedyLCM(arr, n));
</script>
|
Time Complexity: O(N2)
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 geeks!
Last Updated :
15 Jun, 2021
Like Article
Save Article