Array element with minimum sum of absolute differences | Set 2 | Function to find the element with minimum sum of differences between any elements in the array ; Stores the required X and sum of absolute differences ; Calculate sum of array elements ; The sum of absolute differences can 't be greater than sum ; Update res that gives the minimum sum ; If the current difference is less than the previous difference ; Update min_diff and res ; Print the resultant value ; Driver Code
def minimumDiff ( arr , N ) : NEW_LINE INDENT res = arr [ 0 ] NEW_LINE sum1 = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum1 += arr [ i ] NEW_LINE DEDENT min_diff = sum1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( abs ( sum1 - ( arr [ i ] * N ) ) < min_diff ) : NEW_LINE INDENT min_diff = abs ( sum1 - ( arr [ i ] * N ) ) NEW_LINE res = arr [ i ] NEW_LINE DEDENT DEDENT print ( res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE minimumDiff ( arr , N ) NEW_LINE DEDENT