Minimum elements inserted in a sorted array to form an Arithmetic progression | Function to find the greatest common divisor of two numbers ; Function to find the minimum the minimum number of elements required to be inserted into array ; Difference array of consecutive elements of the array ; GCD of the difference array ; Loop to calculate the minimum number of elements required ; Driver Code ; Function calling
def gcdFunc ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcdFunc ( b , a % b ) NEW_LINE DEDENT def findMinimumElements ( a , n ) : NEW_LINE INDENT b = [ 0 ] * ( n - 1 ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT b [ i - 1 ] = a [ i ] - a [ i - 1 ] NEW_LINE DEDENT gcd = b [ 0 ] NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT gcd = gcdFunc ( gcd , b [ i ] ) NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT ans += ( b [ i ] // gcd ) - 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT arr1 = [ 1 , 6 , 8 , 10 , 14 , 16 ] NEW_LINE n1 = len ( arr1 ) NEW_LINE print ( findMinimumElements ( arr1 , n1 ) ) NEW_LINE