Minimum LCM of all subarrays of length at least 2 | Java program for the above approach ; Function to find LCM pf two numbers ; Initialise lcm value ; Check for divisibility of a and b by the lcm ; Function to find the Minimum LCM of all subarrays of length greater than 1 ; Store the minimum LCM ; Traverse the array ; Find LCM of consecutive element ; Check if the calculated LCM is less than the minLCM then update it ; Print the minimum LCM ; Driver Code ; Given array arr [ ] ; Size of the array ; Function call
import java . util . * ; class GFG { static int LCM ( int a , int b ) { int lcm = a > b ? a : b ; while ( true ) { if ( lcm % a == 0 && lcm % b == 0 ) break ; else lcm ++ ; } return lcm ; } static void findMinLCM ( int arr [ ] , int n ) { int minLCM = Integer . MAX_VALUE ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int val = LCM ( arr [ i ] , arr [ i + 1 ] ) ; if ( val < minLCM ) { minLCM = val ; } } System . out . print ( minLCM + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 8 , 12 , 16 , 20 , 24 } ; int n = arr . length ; findMinLCM ( arr , n ) ; } }