Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Maximize difference between maximum and minimum array elements after K operations

  • Last Updated : 08 Apr, 2021

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:

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.

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++




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

Java




// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
     
// Reverse array
static 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 operations
static 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 Code
public 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 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 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));

C#




// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to find the maximum difference
// between the maximum and minimum in the
// array after K operations
static 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 code
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));
}
}
 
// This code is contributed by mohit kumar 29

Javascript




<script>
 
// Javascript program to implement
// the above approach
 
// Reverse array
function 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 operations
function 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 Code
var 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>
Output: 
14

 

Time Complexity: O(N * log(N)) 
Auxiliary Space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :