Minimum elements to be inserted in Array to make adjacent differences equal | Function to find gcd of two numbers ; Function to calculate minimum numbers to be inserted to make equal differences between two consecutive elements ; Check if there is only one element in the array then answer will be 0 ; Calculate difference between first and second element of array ; If there is only two elements in the array then gcd of differences of consecutive elements of array will be equal to difference of first and second element of the array ; Loop to calculate the gcd of the differences between consecutive elements of the array ; Loop to calculate the elements to be inserted ; Return the answer ; Driver code
def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def minimum_elements ( n , arr ) : NEW_LINE INDENT if ( n < 3 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT ans = 0 NEW_LINE diff = arr [ 1 ] - arr [ 0 ] NEW_LINE g = diff NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT diff = arr [ i ] - arr [ i - 1 ] NEW_LINE g = gcd ( g , diff ) NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT diff = arr [ i ] - arr [ i - 1 ] NEW_LINE cnt = diff // g NEW_LINE ans += ( cnt - 1 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 5 , 8 , 10 , 12 , 16 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minimum_elements ( n , arr ) ) NEW_LINE