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 operations 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 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 Code 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; } |
Python3
# Pyhton program to implement # the above approach # Function to find the maximum difference # between the maximum and minimum in the # array after K operations def 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 Code if __name__ == "__main__": arr = [ 7, 7, 7, 7 ]; N = len(arr) K = 1; print(maxDiffLargSmallOper(arr, N, K)); |
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.
Recommended Posts:
- Minimum difference between maximum and minimum value of Array with given Operations
- Maximize difference between the Sum of the two halves of the Array after removal of N elements
- Maximum sum of all elements of array after performing given operations
- Maximize the median of the given array after adding K elements to the same array
- Maximize the sum of differences of consecutive elements after removing exactly K elements
- Maximize sum of given array by rearranging array such that the difference between adjacent elements is atmost 1
- Maximize sum of array elements removed by performing the given operations
- Minimum length subarray containing all unique elements after Q operations
- Find the maximum difference after applying the given operations two times on a number
- Maximize length of Subarray of 1's after removal of a pair of consecutive Array elements
- Maximize sum of remaining elements after every removal of the array half with greater sum
- Find an index such that difference between product of elements before and after it is minimum
- Minimum element left from the array after performing given operations
- Minimum number of operations required to maximize the Binary String
- Maximum value in an array after m range increment operations
- Maximum Possible Product in Array after performing given Operations
- Maximum count of equal numbers in an array after performing given operations
- Find maximum value of the last element after reducing the array with given operations
- Maximum number of unique values in the array after performing given operations
- Maximum score possible after performing given operations on an Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

