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
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function findMaxD ( arr , N , K ) { for ( let i = 0 ; i < N ; i ++ ) { arr [ i ] = Math . abs ( arr [ i ] - K ) ; } let D = arr [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) { D = gcd ( D , arr [ i ] ) ; } return D ; } let arr = [ 1 , 7 , 11 ] ; let N = arr . length ; let K = 3 ; document . write ( findMaxD ( arr , N , K ) ) ;