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 14

Input: 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:



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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));

chevron_right


Output:

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.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

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.