📌  相关文章
📜  可以分成 N 个相等部分的杆的最小长度,该长度可以进一步分成给定数量的相等部分

📅  最后修改于: 2021-09-04 08:10:45             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[] ,任务是找到一根杆的最小可能长度,该长度可以被切割成N 个相等的部分,使得每个i部分都可以被切割成arr[i]等份。

例子:

朴素的方法:可以根据以下观察解决给定的问题:

  • 考虑杆的最小长度为X ,然后将该杆切成N 等份,每部分的长度为X/N
  • 现在每个N 部分将再次被削减如下:
    • 第 1 部分将被切成arr[0]相等,其中每个部分的长度为 a 1
    • 第 2 部分将被切成arr[1]相等,其中每个部分的长度为 a 2
    • 第 3 部分将被切成arr[2]相等,其中每个部分的长度为 a 3
    • .
    • .
    • .
    • 等等。
  • 现在,上面的关系也可以写成:
  • 因此,杆的最小长度由下式给出:

根据上述观察,打印N与给定数组arr[]的 LCM 的乘积值作为所得的杆的最小长度。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find GCD
// of two numbers a and b
int gcd(int a, int b)
{
    // Base Case
    if (b == 0)
        return a;
 
    // Find GCD recursively
    return gcd(b, a % b);
}
 
// Function to find the LCM
// of the resultant array
int findlcm(int arr[], int n)
{
    // Initialize a variable ans
    // as the first element
    int ans = arr[0];
 
    // Traverse the array
    for (int i = 1; i < n; i++) {
 
        // Update LCM
        ans = (((arr[i] * ans))
               / (gcd(arr[i], ans)));
    }
 
    // Return the minimum
    // length of the rod
    return ans;
}
 
// Function to find the minimum length
// of the rod that can be divided into
// N equals parts and each part can be
// further divided into arr[i] equal parts
void minimumRod(int A[], int N)
{
    // Print the result
    cout << N * findlcm(A, N);
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
    minimumRod(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
public class GFG {
 
    // Function to find GCD
    // of two numbers a and b
    static int gcd(int a, int b)
    {
        // Base Case
        if (b == 0)
            return a;
 
        // Find GCD recursively
        return gcd(b, a % b);
    }
 
    // Function to find the LCM
    // of the resultant array
    static int findlcm(int arr[], int n)
    {
        // Initialize a variable ans
        // as the first element
        int ans = arr[0];
 
        // Traverse the array
        for (int i = 1; i < n; i++) {
 
            // Update LCM
            ans = (((arr[i] * ans)) / (gcd(arr[i], ans)));
        }
 
        // Return the minimum
        // length of the rod
        return ans;
    }
 
    // Function to find the minimum length
    // of the rod that can be divided into
    // N equals parts and each part can be
    // further divided into arr[i] equal parts
    static void minimumRod(int A[], int N)
    {
        // Print the result
        System.out.println(N * findlcm(A, N));
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 1, 2 };
        int N = arr.length;
        minimumRod(arr, N);
    }
}
 
// This code is contributed by Kingash.


Python3
# Python3 program for the above approach
 
# Function to find GCD
# of two numbers a and b
def gcd(a, b):
     
    # Base Case
    if (b == 0):
        return a
         
    # Find GCD recursively
    return gcd(b, a % b)
 
# Function to find the LCM
# of the resultant array
def findlcm(arr, n):
     
    # Initialize a variable ans
    # as the first element
    ans = arr[0]
     
    # Traverse the array
    for i in range(n):
         
        # Update LCM
        ans = (((arr[i] * ans)) /
            (gcd(arr[i], ans)))
     
    # Return the minimum
    # length of the rod
    return ans
 
# Function to find the minimum length
# of the rod that can be divided into
# N equals parts and each part can be
# further divided into arr[i] equal parts
def minimumRod(A, N):
     
    # Print the result
    print(int(N * findlcm(A, N)))
 
# Driver Code
arr = [ 1, 2 ]
N = len(arr)
 
minimumRod(arr, N)
 
# This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
class GFG
{
   
    // Function to find GCD
    // of two numbers a and b
    static int gcd(int a, int b)
    {
       
        // Base Case
        if (b == 0)
            return a;
 
        // Find GCD recursively
        return gcd(b, a % b);
    }
 
    // Function to find the LCM
    // of the resultant array
    static int findlcm(int[] arr, int n)
    {
       
        // Initialize a variable ans
        // as the first element
        int ans = arr[0];
 
        // Traverse the array
        for (int i = 1; i < n; i++) {
 
            // Update LCM
            ans = (((arr[i] * ans)) / (gcd(arr[i], ans)));
        }
 
        // Return the minimum
        // length of the rod
        return ans;
    }
 
    // Function to find the minimum length
    // of the rod that can be divided into
    // N equals parts and each part can be
    // further divided into arr[i] equal parts
    static void minimumRod(int[] A, int N)
    {
       
        // Print the result
        Console.WriteLine(N * findlcm(A, N));
    }
   
  // Driver code
    static void Main()
    {
        int[] arr = { 1, 2 };
        int N = arr.Length;
        minimumRod(arr, N);
    }
}
 
// This code is contributed by sk944795.


Javascript


输出:
4

时间复杂度: O(N*log M) 其中 M 是数组最大元素
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live