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++
#include <bits/stdc++.h>
using namespace std;
int maxDiffLargSmallOper( int arr[],
int N, int K)
{
int maxDiff = 0;
sort(arr, arr + N, greater< int >());
for ( int i = 0; i <= min(K, N - 1);
i++) {
maxDiff += arr[i];
}
return maxDiff;
}
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
import java.util.*;
class GFG{
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;
}
static int maxDiffLargSmallOper( int arr[],
int N, int K)
{
int maxDiff = 0 ;
Arrays.sort(arr);
arr = reverse(arr);
for ( int i = 0 ; i <= Math.min(K, N - 1 ); i++)
{
maxDiff += arr[i];
}
return maxDiff;
}
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));
}
}
|
Python3
def maxDiffLargSmallOper(arr, N, K):
maxDiff = 0 ;
arr.sort(reverse = True );
for i in range ( min (K + 1 , N)):
maxDiff + = arr[i];
return maxDiff;
if __name__ = = "__main__" :
arr = [ 7 , 7 , 7 , 7 ];
N = len (arr)
K = 1 ;
print (maxDiffLargSmallOper(arr, N, K));
|
C#
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));
}
}
|
Javascript
<script>
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 maxDiffLargSmallOper(arr, N, K)
{
var maxDiff = 0;
arr.sort();
arr = reverse(arr);
for (i = 0; i <= Math.min(K, N - 1); i++)
{
maxDiff += arr[i];
}
return maxDiff;
}
var arr = [ 7, 7, 7, 7 ];
var N = arr.length;
var K = 1;
document.write(maxDiffLargSmallOper(arr, N, K));
</script>
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
08 Apr, 2021
Like Article
Save Article