Minimum possible value of D which when added to or subtracted from K repeatedly obtains every array element | Recursive function tox previous gcd of a and b ; Function to find the maximum value of D such that every element in the array can be obtained by performing K + D or K - D ; Traverse the array arr [ ] ; Update arr [ i ] ; Stores GCD of the array ; Iterate over the range [ 1 , N ] ; Update the value of D ; Print the value of D ; 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 findMaxD ( arr , N , K ) : NEW_LINE INDENT for i in range ( 0 , N ) : NEW_LINE INDENT arr [ i ] = abs ( arr [ i ] - K ) NEW_LINE DEDENT D = arr [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT D = gcd ( D , arr [ i ] ) NEW_LINE DEDENT return D NEW_LINE DEDENT arr = [ 1 , 7 , 11 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE print ( findMaxD ( arr , N , K ) ) NEW_LINE