📌  相关文章
📜  长度至少为2的所有子阵列的最小LCM

📅  最后修改于: 2021-04-26 09:28:13             🧑  作者: Mango

给定N个正整数的数组arr [] 。任务是找到大小大于1的所有子阵列的最小LCM。

例子:

天真的方法:想法是生成所有长度至少为2的可能子阵列,并找到形成的所有子阵列的LCM。打印所有子阵列中的最小LCM。

时间复杂度: O(N 3 )
辅助空间: O(1)

高效方法:要优化上述方法,我们必须观察到,当且仅当必须计算其LCM的元素数量最小时,两个或多个数字的LCM才会较小。子数组大小的最小可能值为2。因此,其思想是找到所有相邻对的LCM,并打印其中的最小对。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
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


输出:
8

时间复杂度: O(N)
辅助空间: O(1)