- Arrays, LCM, Mathematical

Minimum length of a rod that can be split into N equal parts that can further be split into given number of equal parts

Minimum length of a rod that can be split into N equal parts that can further be split into given number of equal partsGiven 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: 4Explanation: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: 2Naive 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 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