给定一个由N 个正整数组成的数组arr[] ,任务是找到一根杆的最小可能长度,该长度可以被切割成N 个相等的部分,使得每个第i部分都可以被切割成arr[i]等份。
例子:
Input: arr[] = {1, 2}
Output: 4
Explanation:
Consider the length of the rod as 4. Then it can be divided in 2 equal parts, each having length 2.
Now, part 1 can be divided in arr[0](= 1) equal parts of length 2.
Part 2 can be divided in arr[1](= 2) equal parts of length 1.
Therefore, the minimum length of the rod must be 4.
Input: arr[] = {1, 1}
Output: 2
朴素的方法:可以根据以下观察解决给定的问题:
- 考虑杆的最小长度为X ,然后将该杆切成N 等份,每部分的长度为X/N 。
- 现在每个N 部分将再次被削减如下:
- 第 1 部分将被切成arr[0]相等,其中每个部分的长度为 a 1 。
- 第 2 部分将被切成arr[1]相等,其中每个部分的长度为 a 2 。
- 第 3 部分将被切成arr[2]相等,其中每个部分的长度为 a 3 。
- .
- .
- .
- 等等。
- 现在,上面的关系也可以写成:
X/N = arr[0]*a1 = arr[1]*a2 = … = arr[N – 1]*aN.
- 因此,杆的最小长度由下式给出:
N*lcm (arr[0]*a1, arr[1]*a2, …, arr[N – 1]*aN)
根据上述观察,打印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