Maximize difference between maximum and minimum array elements after K operations | Function to find the maximum difference between the maximum and minimum in the array after K operations ; Stores maximum difference between largest and smallest array element ; Sort the array in descending order ; Traverse the array arr [ ] ; Update maxDiff ; Driver Code
def maxDiffLargSmallOper ( arr , N , K ) : NEW_LINE INDENT maxDiff = 0 ; NEW_LINE arr . sort ( reverse = True ) ; NEW_LINE for i in range ( min ( K + 1 , N ) ) : NEW_LINE INDENT maxDiff += arr [ i ] ; NEW_LINE DEDENT return maxDiff ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 7 , 7 , 7 , 7 ] ; NEW_LINE N = len ( arr ) NEW_LINE K = 1 ; NEW_LINE print ( maxDiffLargSmallOper ( arr , N , K ) ) ; NEW_LINE DEDENT