Minimum LCM of all subarrays of length at least 2
Last Updated :
19 Jul, 2023
Given an array arr[] of N positive integers. The task is to find the minimum LCM of all subarrays of size greater than 1.
Examples:
Input: arr[] = { 3, 18, 9, 18, 5, 15, 8, 7, 6, 9 }
Output: 15
Explanation:
LCM of subarray {5, 15} is minimum which is 15.
Input: arr[] = { 4, 8, 12, 16, 20, 24 }
Output: 8
Explanation:
LCM of subarray {4, 8} is minimum which is 8.
Brute Force Approach:
- initialize a variable min_lcm to INT_MAX.
- Next, we use nested loops to generate all possible subarrays of a length greater than 1.
- For each subarray, we calculate its LCM by iterating over its elements and calculating the LCM of each pair of elements using the LCM function defined earlier.
- We compare this LCM with the current minimum LCM and update it if the LCM is smaller.
- Finally, we print the minimum LCM obtained as the output.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int LCM( int a, int b)
{
return (a * b) / __gcd(a, b);
}
void findMinLCM( int arr[], int n)
{
int min_lcm = INT_MAX;
for ( int i = 0; i < n; i++)
{
for ( int j = i+1; j < n; j++)
{
int lcm = arr[i];
for ( int k = i+1; k <= j; k++)
{
lcm = LCM(lcm, arr[k]);
}
if (lcm < min_lcm)
{
min_lcm = lcm;
}
}
}
cout << min_lcm << endl;
}
int main()
{
int arr[] = { 4, 8, 12, 16, 20, 24 };
int n = sizeof (arr) / sizeof (arr[0]);
findMinLCM(arr, n);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int LCM( int a, int b) {
return (a * b) / gcd(a, b);
}
public static int gcd( int a, int b) {
if (b == 0 ) {
return a;
} else {
return gcd(b, a % b);
}
}
public static void findMinLCM( int [] arr, int n) {
int min_lcm = Integer.MAX_VALUE;
for ( int i = 0 ; i < n; i++) {
for ( int j = i+ 1 ; j < n; j++) {
int lcm = arr[i];
for ( int k = i+ 1 ; k <= j; k++) {
lcm = LCM(lcm, arr[k]);
}
if (lcm < min_lcm) {
min_lcm = lcm;
}
}
}
System.out.println(min_lcm);
}
public static void main(String[] args) {
int [] arr = { 4 , 8 , 12 , 16 , 20 , 24 };
int n = arr.length;
findMinLCM(arr, n);
}
}
|
Python3
import math
def LCM(a, b):
return (a * b) / / math.gcd(a, b)
def findMinLCM(arr, n):
min_lcm = float ( 'inf' )
for i in range (n):
for j in range (i + 1 , n):
lcm = arr[i]
for k in range (i + 1 , j + 1 ):
lcm = LCM(lcm, arr[k])
if lcm < min_lcm:
min_lcm = lcm
print (min_lcm)
if __name__ = = '__main__' :
arr = [ 4 , 8 , 12 , 16 , 20 , 24 ]
n = len (arr)
findMinLCM(arr, n)
|
C#
using System;
public class Program
{
public static int LCM( int a, int b)
{
return (a * b) / GCD(a, b);
}
public static int GCD( int a, int b)
{
if (a == 0) return b;
return GCD(b % a, a);
}
public static void FindMinLCM( int [] arr, int n)
{
int min_lcm = int .MaxValue;
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
int lcm = arr[i];
for ( int k = i + 1; k <= j; k++)
{
lcm = LCM(lcm, arr[k]);
}
if (lcm < min_lcm)
{
min_lcm = lcm;
}
}
}
Console.WriteLine(min_lcm);
}
public static void Main()
{
int [] arr = { 4, 8, 12, 16, 20, 24 };
int n = arr.Length;
FindMinLCM(arr, n);
}
}
|
Javascript
function LCM(a, b) {
return (a * b) / gcd(a, b);
}
function gcd(a, b) {
if (b === 0) {
return a;
} else {
return gcd(b, a % b);
}
}
function findMinLCM(arr) {
let min_lcm = Number.MAX_VALUE;
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
let lcm = arr[i];
for (let k = i + 1; k <= j; k++) {
lcm = LCM(lcm, arr[k]);
}
if (lcm < min_lcm) {
min_lcm = lcm;
}
}
}
console.log(min_lcm);
}
const arr = [4, 8, 12, 16, 20, 24];
findMinLCM(arr);
|
Time Complexity: O(N^3)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach we have to observe that the LCM of two or more numbers will be less if and only if the number of elements whose LCM has to be calculated is minimum. The minimum possible value for subarray size is 2. Therefore the idea is to find the LCM of all the adjacent pairs and print the minimum of them.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int LCM( int a, int b)
{
int lcm = a > b ? a : b;
while ( true ) {
if (lcm % a == 0 && lcm % b == 0)
break ;
else
lcm++;
}
return lcm;
}
void findMinLCM( int arr[], int n)
{
int minLCM = INT_MAX;
for ( int i = 0; i < n - 1; i++) {
int val = LCM(arr[i], arr[i + 1]);
if (val < minLCM) {
minLCM = val;
}
}
cout << minLCM << endl;
}
int main()
{
int arr[] = { 4, 8, 12, 16, 20, 24 };
int n = sizeof (arr) / sizeof (arr[0]);
findMinLCM(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int LCM( int a, int b)
{
int lcm = a > b ? a : b;
while ( true )
{
if (lcm % a == 0 && lcm % b == 0 )
break ;
else
lcm++;
}
return lcm;
}
static void findMinLCM( int arr[], int n)
{
int minLCM = Integer.MAX_VALUE;
for ( int i = 0 ; i < n - 1 ; i++)
{
int val = LCM(arr[i], arr[i + 1 ]);
if (val < minLCM)
{
minLCM = val;
}
}
System.out.print(minLCM + "\n" );
}
public static void main(String[] args)
{
int arr[] = { 4 , 8 , 12 , 16 , 20 , 24 };
int n = arr.length;
findMinLCM(arr, n);
}
}
|
Python3
import sys
def LCM(a, b):
lcm = a if a > b else b
while ( True ):
if (lcm % a = = 0 and lcm % b = = 0 ):
break
else :
lcm + = 1
return lcm
def findMinLCM(arr, n):
minLCM = sys.maxsize
for i in range (n - 1 ):
val = LCM(arr[i], arr[i + 1 ])
if (val < minLCM):
minLCM = val
print (minLCM)
arr = [ 4 , 8 , 12 , 16 , 20 , 24 ]
n = len (arr)
findMinLCM(arr, n)
|
C#
using System;
class GFG{
static int LCM( int a, int b)
{
int lcm = a > b ? a : b;
while ( true )
{
if (lcm % a == 0 && lcm % b == 0)
break ;
else
lcm++;
}
return lcm;
}
static void findMinLCM( int []arr, int n)
{
int minLCM = int .MaxValue;
for ( int i = 0; i < n - 1; i++)
{
int val = LCM(arr[i], arr[i + 1]);
if (val < minLCM)
{
minLCM = val;
}
}
Console.Write(minLCM + "\n" );
}
public static void Main(String[] args)
{
int []arr = { 4, 8, 12, 16, 20, 24 };
int n = arr.Length;
findMinLCM(arr, n);
}
}
|
Javascript
<script>
function LCM(a, b)
{
let lcm = a > b ? a : b;
while ( true ) {
if (lcm % a == 0 && lcm % b == 0)
break ;
else
lcm++;
}
return lcm;
}
function findMinLCM(arr, n)
{
let minLCM = Number.MAX_VALUE;
for (let i = 0; i < n - 1; i++) {
let val = LCM(arr[i], arr[i + 1]);
if (val < minLCM) {
minLCM = val;
}
}
document.write(minLCM + "<br>" );
}
let arr = [ 4, 8, 12, 16, 20, 24 ];
let n = arr.length;
findMinLCM(arr, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...