Maximum value of X such that difference between any array element and X does not exceed K | Function to find maximum value of X such that | A [ i ] - X | a K ; Stores the smallest array element ; Store the possible value of X ; Traverse the array A [ ] ; If required criteria is not satisfied ; Update ans ; Print the result ; Driver Code
def maximumNumber ( arr , N , K ) : NEW_LINE INDENT minimum = min ( arr ) NEW_LINE ans = minimum + K NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( abs ( arr [ i ] - ans ) > K ) : NEW_LINE INDENT ans = - 1 NEW_LINE break NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 5 ] NEW_LINE K = 2 NEW_LINE N = len ( arr ) NEW_LINE maximumNumber ( arr , N , K ) NEW_LINE DEDENT