Minimum length of a rod that can be split into N equal parts that can further be split into given number of equal parts | Java program for the above approach ; Function to find GCD of two numbers a and b ; Base Case ; Find GCD recursively ; Function to find the LCM of the resultant array ; Initialize a variable ans as the first element ; Traverse the array ; Update LCM ; Return the minimum length of the rod ; 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 ; Print the result ; Driver Code
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 ) ; } }