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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the LCM of 2 numbers int LCM( int a, int b) { return (a * b) / __gcd(a, b); } // Function to find the Minimum LCM of // all subarrays of length greater than 1 void findMinLCM( int arr[], int n) { int min_lcm = INT_MAX; // Generate all possible subarrays 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; } // Driver Code int main() { // Given array arr[] int arr[] = { 4, 8, 12, 16, 20, 24 }; // Size of the array int n = sizeof (arr) / sizeof (arr[0]); // Function Call findMinLCM(arr, n); return 0; } |
Java
import java.util.*; public class Main { // Function to find the LCM of 2 numbers public static int LCM( int a, int b) { return (a * b) / gcd(a, b); } // Function to find the GCD of 2 numbers public static int gcd( int a, int b) { if (b == 0 ) { return a; } else { return gcd(b, a % b); } } // Function to find the Minimum LCM of // all subarrays of length greater than 1 public static void findMinLCM( int [] arr, int n) { int min_lcm = Integer.MAX_VALUE; // Generate all possible subarrays 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); } // Driver Code public static void main(String[] args) { // Given array arr[] int [] arr = { 4 , 8 , 12 , 16 , 20 , 24 }; // Size of the array int n = arr.length; // Function Call findMinLCM(arr, n); } } |
Python3
# Python program for the above approach # Function to find the LCM of 2 numbers import math def LCM(a, b): return (a * b) / / math.gcd(a, b) # Function to find the Minimum LCM of # all subarrays of length greater than 1 def findMinLCM(arr, n): min_lcm = float ( 'inf' ) # Generate all possible subarrays 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) # Driver Code if __name__ = = '__main__' : # Given array arr[] arr = [ 4 , 8 , 12 , 16 , 20 , 24 ] # Size of the array n = len (arr) # Function Call findMinLCM(arr, n) |
C#
using System; public class Program { // Function to find the LCM of 2 numbers public static int LCM( int a, int b) { return (a * b) / GCD(a, b); } // Function to find the GCD of 2 numbers public static int GCD( int a, int b) { if (a == 0) return b; return GCD(b % a, a); } // Function to find the Minimum LCM of // all subarrays of length greater than 1 public static void FindMinLCM( int [] arr, int n) { int min_lcm = int .MaxValue; // Generate all possible subarrays 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() { // Given array arr[] int [] arr = { 4, 8, 12, 16, 20, 24 }; // Size of the array int n = arr.Length; // Function Call FindMinLCM(arr, n); } } |
Javascript
// Function to find the LCM of 2 numbers function LCM(a, b) { return (a * b) / gcd(a, b); } // Function to find the GCD of 2 numbers function gcd(a, b) { if (b === 0) { return a; } else { return gcd(b, a % b); } } // Function to find the Minimum LCM of // all subarrays of length greater than 1 function findMinLCM(arr) { let min_lcm = Number.MAX_VALUE; // Generate all possible subarrays 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); } // Driver Code // Given array arr[] const arr = [4, 8, 12, 16, 20, 24]; // Function Call findMinLCM(arr); |
8
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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find LCM pf two numbers int LCM( int a, int b) { // Initialise lcm value int lcm = a > b ? a : b; while ( true ) { // Check for divisibility // of a and b by the lcm if (lcm % a == 0 && lcm % b == 0) break ; else lcm++; } return lcm; } // Function to find the Minimum LCM of // all subarrays of length greater than 1 void findMinLCM( int arr[], int n) { // Store the minimum LCM int minLCM = INT_MAX; // Traverse the array for ( int i = 0; i < n - 1; i++) { // Find LCM of consecutive element int val = LCM(arr[i], arr[i + 1]); // Check if the calculated LCM is // less than the minLCM then update it if (val < minLCM) { minLCM = val; } } // Print the minimum LCM cout << minLCM << endl; } // Driver Code int main() { // Given array arr[] int arr[] = { 4, 8, 12, 16, 20, 24 }; // Size of the array int n = sizeof (arr) / sizeof (arr[0]); // Function Call findMinLCM(arr, n); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to find LCM pf two numbers static int LCM( int a, int b) { // Initialise lcm value int lcm = a > b ? a : b; while ( true ) { // Check for divisibility // of a and b by the lcm if (lcm % a == 0 && lcm % b == 0 ) break ; else lcm++; } return lcm; } // Function to find the Minimum LCM of // all subarrays of length greater than 1 static void findMinLCM( int arr[], int n) { // Store the minimum LCM int minLCM = Integer.MAX_VALUE; // Traverse the array for ( int i = 0 ; i < n - 1 ; i++) { // Find LCM of consecutive element int val = LCM(arr[i], arr[i + 1 ]); // Check if the calculated LCM is // less than the minLCM then update it if (val < minLCM) { minLCM = val; } } // Print the minimum LCM System.out.print(minLCM + "\n" ); } // Driver Code public static void main(String[] args) { // Given array arr[] int arr[] = { 4 , 8 , 12 , 16 , 20 , 24 }; // Size of the array int n = arr.length; // Function call findMinLCM(arr, n); } } // This code is contributed by amal kumar choubey |
Python3
# Python3 program for the above approach import sys # Function to find LCM pf two numbers def LCM(a, b): # Initialise lcm value lcm = a if a > b else b while ( True ): # Check for divisibility # of a and b by the lcm if (lcm % a = = 0 and lcm % b = = 0 ): break else : lcm + = 1 return lcm # Function to find the Minimum LCM of # all subarrays of length greater than 1 def findMinLCM(arr, n): # Store the minimum LCM minLCM = sys.maxsize # Traverse the array for i in range (n - 1 ): # Find LCM of consecutive element val = LCM(arr[i], arr[i + 1 ]) # Check if the calculated LCM is # less than the minLCM then update it if (val < minLCM): minLCM = val # Print the minimum LCM print (minLCM) # Driver Code # Given array arr[] arr = [ 4 , 8 , 12 , 16 , 20 , 24 ] # Size of the array n = len (arr) # Function call findMinLCM(arr, n) # This code is contributed by sanjoy_62 |
C#
// C# program for the above approach using System; class GFG{ // Function to find LCM pf two numbers static int LCM( int a, int b) { // Initialise lcm value int lcm = a > b ? a : b; while ( true ) { // Check for divisibility // of a and b by the lcm if (lcm % a == 0 && lcm % b == 0) break ; else lcm++; } return lcm; } // Function to find the Minimum LCM of // all subarrays of length greater than 1 static void findMinLCM( int []arr, int n) { // Store the minimum LCM int minLCM = int .MaxValue; // Traverse the array for ( int i = 0; i < n - 1; i++) { // Find LCM of consecutive element int val = LCM(arr[i], arr[i + 1]); // Check if the calculated LCM is // less than the minLCM then update it if (val < minLCM) { minLCM = val; } } // Print the minimum LCM Console.Write(minLCM + "\n" ); } // Driver Code public static void Main(String[] args) { // Given array []arr int []arr = { 4, 8, 12, 16, 20, 24 }; // Size of the array int n = arr.Length; // Function call findMinLCM(arr, n); } } // This code is contributed by PrinciRaj1992 |
Javascript
<script> // Javascript program for the above approach // Function to find LCM of two numbers function LCM(a, b) { // Initialise lcm value let lcm = a > b ? a : b; while ( true ) { // Check for divisibility // of a and b by the lcm if (lcm % a == 0 && lcm % b == 0) break ; else lcm++; } return lcm; } // Function to find the Minimum LCM of // all subarrays of length greater than 1 function findMinLCM(arr, n) { // Store the minimum LCM let minLCM = Number.MAX_VALUE; // Traverse the array for (let i = 0; i < n - 1; i++) { // Find LCM of consecutive element let val = LCM(arr[i], arr[i + 1]); // Check if the calculated LCM is // less than the minLCM then update it if (val < minLCM) { minLCM = val; } } // Print the minimum LCM document.write(minLCM + "<br>" ); } // Driver Code // Given array arr[] let arr = [ 4, 8, 12, 16, 20, 24 ]; // Size of the array let n = arr.length; // Function Call findMinLCM(arr, n); // This code is contributed by Mayank Tyagi </script> |
8
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!