Maximize difference between maximum and minimum array elements after K operations
Given an array arr[] of size N and a positive integer K, the task is to find the maximum difference between the largest element and the smallest element in the array by incrementing or decrementing array elements by 1, K times.
Examples:
Input: arr[] = {7, 7, 7, 7}, K = 1
Output: 14
Explanation: Decrementing the value of arr[0] and incrementing the value of arr[3] by 7 modifies arr[] = {0, 7, 7, 14}. Therefore, the maximum difference between the largest element and the smallest element of the array is 14Input: arr[] = {0, 0, 0, 0, 0}, K = 2
Output: 0
Explanation: Since all array elements are 0, decrementing any array element makes that element less than 0. Therefore, the required output is 0.
Approach: Follow the steps below to solve the problem:
- Sort the array in descending order.
- Traverse the array and calculate the sum of first K the largest elements.
- Finally, print the sum of first K the largest elements of the array.
Below is the implementation of the above approach:
C++
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;// Function to find the maximum difference// between the maximum and minimum in the// array after K operationsint maxDiffLargSmallOper(int arr[], int N, int K){ // Stores maximum difference between // largest and smallest array element int maxDiff = 0; // Sort the array in descending order sort(arr, arr + N, greater<int>()); // Traverse the array arr[] for (int i = 0; i <= min(K, N - 1); i++) { // Update maxDiff maxDiff += arr[i]; } return maxDiff;}// Driver Codeint main(){ int arr[] = { 7, 7, 7, 7 }; int N = sizeof(arr) / sizeof(arr[0]); int K = 1; cout << maxDiffLargSmallOper(arr, N, K); return 0;} |
Java
// Java program to implement// the above approachimport java.util.*;class GFG{ // Reverse arraystatic int[] reverse(int a[]){ int i, n = a.length, t; for(i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } return a;}// Function to find the maximum difference// between the maximum and minimum in the// array after K operationsstatic int maxDiffLargSmallOper(int arr[], int N, int K){ // Stores maximum difference between // largest and smallest array element int maxDiff = 0; // Sort the array in descending order Arrays.sort(arr); arr = reverse(arr); // Traverse the array arr[] for(int i = 0; i <= Math.min(K, N - 1); i++) { // Update maxDiff maxDiff += arr[i]; } return maxDiff;}// Driver Codepublic static void main(String[] args){ int arr[] = { 7, 7, 7, 7 }; int N = arr.length; int K = 1; System.out.print(maxDiffLargSmallOper(arr, N, K));}}// This code is contributed by Amit Katiyar |
Python3
# Python3 program to implement# the above approach# Function to find the maximum difference# between the maximum and minimum in the# array after K operationsdef maxDiffLargSmallOper(arr, N, K): # Stores maximum difference between # largest and smallest array element maxDiff = 0; # Sort the array in descending order arr.sort(reverse = True); # Traverse the array arr[] for i in range(min(K + 1, N)): # Update maxDiff maxDiff += arr[i]; return maxDiff;# Driver Codeif __name__ == "__main__": arr = [ 7, 7, 7, 7 ]; N = len(arr) K = 1; print(maxDiffLargSmallOper(arr, N, K)); |
C#
// C# program to implement// the above approachusing System;class GFG{// Function to find the maximum difference// between the maximum and minimum in the// array after K operationsstatic int maxDiffLargSmallOper(int []arr, int N, int K){ // Stores maximum difference between // largest and smallest array element int maxDiff = 0; // Sort the array in descending order Array.Sort(arr); Array.Reverse(arr); // Traverse the array arr[] for(int i = 0; i <= Math.Min(K, N - 1); i++) { // Update maxDiff maxDiff += arr[i]; } return maxDiff;}// Driver codepublic static void Main(){ int [] arr = new int[]{ 7, 7, 7, 7 }; int N = arr.Length; int K = 1; Console.Write(maxDiffLargSmallOper(arr, N, K));}}// This code is contributed by mohit kumar 29 |
Javascript
<script>// Javascript program to implement// the above approach// Reverse arrayfunction reverse(a){ var i, n = a.length, t; for(i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } return a;}// Function to find the maximum difference// between the maximum and minimum in the// array after K operationsfunction maxDiffLargSmallOper(arr, N, K){ // Stores maximum difference between // largest and smallest array element var maxDiff = 0; // Sort the array in descending order arr.sort(); arr = reverse(arr); // Traverse the array arr for(i = 0; i <= Math.min(K, N - 1); i++) { // Update maxDiff maxDiff += arr[i]; } return maxDiff;}// Driver Codevar arr = [ 7, 7, 7, 7 ];var N = arr.length;var K = 1;document.write(maxDiffLargSmallOper(arr, N, K));// This code is contributed by aashish1995</script> |
14
Time Complexity: O(N * log(N))
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


