Minimum length of a rod that can be split into N equal parts that can further be split into given number of equal parts
Given an array arr[] consisting of N positive integers, the task is to find the minimum possible length of a rod that can be cut into N equal parts such that every ith part can be cut into arr[i] equal parts.
Examples:
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
Naive Approach: The given problem can be solved based on the following observations:
- Consider the minimum length of the rod is X, then this rod is cut into N equal parts and the length of each part will be X/N.
- Now each N parts will again be cut down as follows:
- Part 1 will be cut into arr[0] equal where each part has a length say a1.
- Part 2 will be cut into arr[1] equal where each part has a length say a2.
- Part 3 will be cut into arr[2] equal where each part has a length say a3.
- .
- .
- .
- and so on.
- Now, the above relation can also be written as:
X/N = arr[0]*a1 = arr[1]*a2 = … = arr[N – 1]*aN.
- Therefore, the minimum length of the rod is given by:
N*lcm (arr[0]*a1, arr[1]*a2, …, arr[N – 1]*aN)
From the above observations, print the value of the product of N and the LCM of the given array arr[] as the resultant minimum length of the rod.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int findlcm( int arr[], int n)
{
int ans = arr[0];
for ( int i = 1; i < n; i++) {
ans = (((arr[i] * ans))
/ (gcd(arr[i], ans)));
}
return ans;
}
void minimumRod( int A[], int N)
{
cout << N * findlcm(A, N);
}
int main()
{
int arr[] = { 1, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
minimumRod(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int findlcm( int arr[], int n)
{
int ans = arr[ 0 ];
for ( int i = 1 ; i < n; i++) {
ans = (((arr[i] * ans)) / (gcd(arr[i], ans)));
}
return ans;
}
static void minimumRod( int A[], int N)
{
System.out.println(N * findlcm(A, N));
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 };
int N = arr.length;
minimumRod(arr, N);
}
}
|
Python3
def gcd(a, b):
if (b = = 0 ):
return a
return gcd(b, a % b)
def findlcm(arr, n):
ans = arr[ 0 ]
for i in range (n):
ans = (((arr[i] * ans)) /
(gcd(arr[i], ans)))
return ans
def minimumRod(A, N):
print ( int (N * findlcm(A, N)))
arr = [ 1 , 2 ]
N = len (arr)
minimumRod(arr, N)
|
C#
using System;
class GFG
{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int findlcm( int [] arr, int n)
{
int ans = arr[0];
for ( int i = 1; i < n; i++) {
ans = (((arr[i] * ans)) / (gcd(arr[i], ans)));
}
return ans;
}
static void minimumRod( int [] A, int N)
{
Console.WriteLine(N * findlcm(A, N));
}
static void Main()
{
int [] arr = { 1, 2 };
int N = arr.Length;
minimumRod(arr, N);
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function findlcm(arr, n)
{
let ans = arr[0];
for (let i = 1; i < n; i++) {
ans = (((arr[i] * ans)) / (gcd(arr[i], ans)));
}
return ans;
}
function minimumRod(A, N)
{
document.write(N * findlcm(A, N));
}
let arr = [ 1, 2 ];
let N = arr.length;
minimumRod(arr, N);
</script>
|
Time Complexity: O(N*log M) where M is the maximum element of the array.
Auxiliary Space: O(1)
Last Updated :
05 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...