Minimum LCM and GCD possible among all possible sub-arrays

Given an array arr[] of N positive integers, the task is to find the minimum LCM and GCD between elements of all possible sub-array.

Examples:

Input: arr[] = {4, 4, 8}
Output: LCM = 4, GCD = 4
All possible sub-arrays are:
{4} -> LCM = 4, GCD = 4
{8} -> LCM = 8, GCD = 8
{4, 8} -> LCM = 8, GCD = 4

Input: arr[] = {2, 66, 14, 521}
Output: LCM = 2, GCD = 1



Approach: We have to approach this problem greedily. It is obvious that when we decrease the number of elements then the LCM will become smaller and when we increase the number of elements then the GCD will become smaller. So we will take the smallest element of the array which is a single element and will be the required LCM. Now for GCD, minimum GCD will be the GCD of all the elements of the array.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return minimum GCD
// among all subarrays
int minGCD(int arr[], int n)
{
  
    int minGCD = 0;
  
    // Minimum GCD among all sub-arrays will be
    // the GCD of all the elements of the array
    for (int i = 0; i < n; i++)
        minGCD = __gcd(minGCD, arr[i]);
  
    return minGCD;
}
  
// Function to return minimum LCM
// among all subarrays
int minLCM(int arr[], int n)
{
  
    int minLCM = arr[0];
  
    // Minimum LCM among all sub-arrays will be
    // the minimum element from the array
    for (int i = 1; i < n; i++)
        minLCM = min(minLCM, arr[i]);
  
    return minLCM;
}
  
// Driver code
int main()
{
  
    int arr[] = { 2, 66, 14, 521 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << "LCM = " << minLCM(arr, n)
         << ", GCD = " << minGCD(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
// Function to return minimum GCD
// among all subarrays
static int __gcd(int a, int b) 
    if (a == 0
        return b; 
    return __gcd(b % a, a); 
static int minGCD(int arr[], int n)
{
  
    int minGCD = 0;
  
    // Minimum GCD among all sub-arrays will be
    // the GCD of all the elements of the array
    for (int i = 0; i < n; i++)
        minGCD = __gcd(minGCD, arr[i]);
  
    return minGCD;
}
  
// Function to return minimum LCM
// among all subarrays
static int minLCM(int arr[], int n)
{
  
    int minLCM = arr[0];
  
    // Minimum LCM among all sub-arrays will be
    // the minimum element from the array
    for (int i = 1; i < n; i++)
        minLCM = Math.min(minLCM, arr[i]);
  
    return minLCM;
}
  
// Driver code
public static void main(String[] args)
{
  
    int arr[] = { 2, 66, 14, 521 };
    int n = arr.length;
  
    System.out.println("LCM = " + minLCM(arr, n)
        + " GCD = "+minGCD(arr, n));
  
}
}
// This code is contributed by Code_Mech.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
from math import gcd
  
# Function to return minimum GCD 
# among all subarrays 
def minGCD(arr, n) :
  
    minGCD = 0;
      
    # Minimum GCD among all sub-arrays 
    # will be the GCD of all the elements
    # of the array
    for i in range(n) :
        minGCD = gcd(minGCD, arr[i]);
          
    return minGCD; 
  
# Function to return minimum LCM 
# among all subarrays 
def minLCM(arr, n) :
  
    minLCM = arr[0]; 
  
    # Minimum LCM among all sub-arrays  
    # will be the minimum element from 
    # the array 
    for i in range(1, n) : 
        minLCM = min(minLCM, arr[i]); 
  
    return minLCM; 
  
# Driver code 
if __name__ == "__main__"
  
    arr = [ 2, 66, 14, 521 ]; 
    n = len(arr); 
  
    print("LCM = ", minLCM(arr, n), 
          ", GCD =", minGCD(arr, n)); 
  
# This code is contributed by Ryuga

chevron_right


C#

// C# implementation of the approach
using System;

class GFG
{
// Function to return minimum GCD
// among all subarrays
static int __gcd(int a, int b)
{
if (a == 0)
return b;
return __gcd(b % a, a);
}
static int minGCD(int [] arr, int n)
{
int minGCD = 0;

// Minimum GCD among all sub-arrays
// will be the GCD of all the
// elements of the array
for (int i = 0; i < n; i++) minGCD = __gcd(minGCD, arr[i]); return minGCD; } // Function to return minimum LCM // among all subarrays static int minLCM(int [] arr, int n) { int minLCM = arr[0]; // Minimum LCM among all sub-arrays // will be the minimum element from // the array for (int i = 1; i < n; i++) minLCM = Math.Min(minLCM, arr[i]); return minLCM; } // Driver code public static void Main() { int [] arr = { 2, 66, 14, 521 }; int n = arr.Length; Console.WriteLine("LCM = " + minLCM(arr, n) + ", GCD = " + minGCD(arr, n)); } } // This code is contributed by ihritik. [tabby title="PHP"]

Output:

LCM = 2, GCD = 1

Time Complexity: O(N)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : AnkitRai01, Code_Mech, ihritik