Maximize difference between maximum and minimum array elements after K operations | C ++ program to implement the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxDiffLargSmallOper ( int arr [ ] , int N , int K ) { int maxDiff = 0 ; sort ( arr , arr + N , greater < int > ( ) ) ; for ( int i = 0 ; i <= min ( K , N - 1 ) ; i ++ ) { maxDiff += arr [ i ] ; } return maxDiff ; } int main ( ) { int arr [ ] = { 7 , 7 , 7 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 1 ; cout << maxDiffLargSmallOper ( arr , N , K ) ; return 0 ; }