📜  所有可能的子阵列中可能的最小 LCM 和 GCD

📅  最后修改于: 2021-10-25 06:51:24             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[] ,任务是在所有可能的子数组的元素之间找到最小的 LCM 和 GCD。
例子:

处理方法:我们必须贪婪地处理这个问题。很明显,当我们减少元素数量时,LCM 会变小,当我们增加元素数量时,GCD 会变小。所以我们将取数组中最小的元素,它是单个元素,将是所需的 LCM。现在对于 GCD,最小 GCD 将是数组所有元素的 GCD。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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;
}


Java
// 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.


Python3
# 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


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.


PHP


Javascript


输出:

LCM = 2, GCD = 1

时间复杂度: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程