Minimum LCM of all subarrays of length at least 2 | Function to find LCM of 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 ; Given array arr [ ] ; Size of the array ; Function Call
function LCM ( a , b ) { let lcm = a > b ? a : b ; while ( true ) { if ( lcm % a == 0 && lcm % b == 0 ) break ; else lcm ++ ; } return lcm ; } function findMinLCM ( arr , n ) { let minLCM = Number . MAX_VALUE ; for ( let i = 0 ; i < n - 1 ; i ++ ) { let val = LCM ( arr [ i ] , arr [ i + 1 ] ) ; if ( val < minLCM ) { minLCM = val ; } } document . write ( minLCM + " " ) ; } let arr = [ 4 , 8 , 12 , 16 , 20 , 24 ] ; let n = arr . length ; findMinLCM ( arr , n ) ;