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
using System ; class GFG { static int maxDiffLargSmallOper ( int [ ] arr , int N , int K ) { int maxDiff = 0 ; Array . Sort ( arr ) ; Array . Reverse ( arr ) ; for ( int i = 0 ; i <= Math . Min ( K , N - 1 ) ; i ++ ) { maxDiff += arr [ i ] ; } return maxDiff ; } public static void Main ( ) { int [ ] arr = new int [ ] { 7 , 7 , 7 , 7 } ; int N = arr . Length ; int K = 1 ; Console . Write ( maxDiffLargSmallOper ( arr , N , K ) ) ; } }