Minimum LCM of all subarrays of length at least 2 | Python3 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 ; Given array arr [ ] ; Size of the array ; Function call
import sys NEW_LINE def LCM ( a , b ) : NEW_LINE INDENT lcm = a if a > b else b NEW_LINE while ( True ) : NEW_LINE INDENT if ( lcm % a == 0 and lcm % b == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT else : NEW_LINE INDENT lcm += 1 NEW_LINE DEDENT DEDENT return lcm NEW_LINE DEDENT def findMinLCM ( arr , n ) : NEW_LINE INDENT minLCM = sys . maxsize NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT val = LCM ( arr [ i ] , arr [ i + 1 ] ) NEW_LINE if ( val < minLCM ) : NEW_LINE INDENT minLCM = val NEW_LINE DEDENT DEDENT print ( minLCM ) NEW_LINE DEDENT arr = [ 4 , 8 , 12 , 16 , 20 , 24 ] NEW_LINE n = len ( arr ) NEW_LINE findMinLCM ( arr , n ) NEW_LINE