# Maximise difference between largest and smallest in Array by shifting X value in pairs after at most K operations

Given an array arr[] of size N and a positive integer K, the task is to maximise difference between the largest element and the smallest element in the array by performing at most K operations, wherein each operation:

• select two integers arr[i] and arr[j] from the array and
• update arr[i] to arr[i] – X and arr[j] to arr[j] + X, where X is any integer in range [0, min(arr[i], arr[j])].

Examples:

Input: arr[] = {3, 3, 2, 3, 3}, K = 1
Output: 6
Explanation: In the 1st operation select the integers (arr, arr) and X as 3. Hence arr = arr – X = 3 – 3 = 0. Similarly, arr = arr + X = 3 + 3 = 6. Hence, arr[] = {6, 0, 2, 3, 3} and the diffecence between smallest and largest element is 6 which is the maximum possible.

Input: arr[] = {7, 4, 8, 11, 2, 23, 67, 22, 5, 29, 6, 4, 56}, N = 13, X = 5
Output: 208

Approach: The given problem can be solved by using a greedy approach. The idea is to maximize the value of the largest element of the array. It can also be observed that the smallest element that can be achieved in the array is 0. Below are the steps to follow:

• Sort the given array arr[] in descending order.
• Iterate through the array in the range [1, N) and perform the given operation on (arr[i], arr) for X = arr[i].
• After K operations have been performed, arr will be the required answer, except for the case where K =0.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ` `using` `namespace` `std;`   `// Function to find maximum difference` `// between the largest and smallest` `// element of arr[] after K operations` `int` `maxDifference(``int` `arr[], ``int` `N, ``int` `K)` `{` `    ``// Sort A in descending order` `    ``sort(arr, arr + N, greater<``int``>());`   `    ``// Case where no operation` `    ``// is to be performed` `    ``if` `(K == 0) {` `        ``return` `arr - arr[N - 1];` `    ``}`   `    ``// Loop to iterate arr[]` `    ``for` `(``int` `i = 1; i < N && K != 0; ++i) {`   `        ``// Update arr` `        ``// and arr[i]` `        ``arr += arr[i];` `        ``arr[i] = 0;`   `        ``// Decrement K` `        ``K--;` `    ``}`   `    ``// Return Answer` `    ``return` `arr;` `}`   `// Driver Code` `int` `main()` `{`   `    ``int` `arr[] = { 3, 3, 2, 3, 3 };` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);` `    ``int` `K = 1;`   `    ``cout << maxDifference(arr, N, K);`   `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `import` `java.io.*;` `import` `java.lang.*;` `import` `java.util.*;`   `class` `GFG {`   `  ``// Function to find maximum difference` `  ``// between the largest and smallest` `  ``// element of arr[] after K operations` `  ``static` `int` `maxDifference(Integer[] arr, ``int` `N, ``int` `K)` `  ``{` `    `  `    ``// Sort A in descending order` `    ``Arrays.sort(arr, Collections.reverseOrder());`   `    ``// Case where no operation` `    ``// is to be performed` `    ``if` `(K == ``0``) {` `      ``return` `arr[``0``] - arr[N - ``1``];` `    ``}`   `    ``// Loop to iterate arr[]` `    ``for` `(``int` `i = ``1``; i < N && K != ``0``; ++i) {`   `      ``// Update arr` `      ``// and arr[i]` `      ``arr[``0``] += arr[i];` `      ``arr[i] = ``0``;`   `      ``// Decrement K` `      ``K--;` `    ``}`   `    ``// Return Answer` `    ``return` `arr[``0``];` `  ``}`   `  ``// Driver Code` `  ``public` `static` `void` `main (String[] args) {` `    ``Integer[] arr = { ``3``, ``3``, ``2``, ``3``, ``3` `};` `    ``int` `N = arr.length;` `    ``int` `K = ``1``;`   `    ``System.out.print(maxDifference(arr, N, K)); ` `  ``}` `}`   `// This code is contributed by hrithikgarg03188.`

## Python3

 `# Python program for the above approach`   `# Function to find maximum difference` `# between the largest and smallest` `# element of arr[] after K operations` `def` `maxDifference(arr, N, K):`   `    ``# Sort A in descending order` `    ``arr.sort(reverse ``=` `True``)`   `    ``# Case where no operation` `    ``# is to be performed` `    ``if` `(K ``=``=` `0``):` `        ``return` `arr[``0``] ``-` `arr[N ``-` `1``]`   `    ``# Loop to iterate arr[]` `    ``for` `i ``in` `range``(``1``, N):`   `        ``# Update arr` `        ``# and arr[i]` `        ``if``(K !``=` `0``):` `            ``arr[``0``] ``=` `arr[``0``] ``+` `arr[i]` `            ``arr[i] ``=` `0`   `            ``# Decrement K` `            ``K ``=` `K ``-` `1`   `    ``# Return Answer` `    ``return` `arr[``0``]`   `# Driver Code` `arr ``=` `[``3``, ``3``, ``2``, ``3``, ``3``]` `N ``=` `len``(arr)` `K ``=` `1`   `print``(maxDifference(arr, N, K))`   `# This code is contributed by Taranpreet`

## C#

 `// C# program to implement` `// the above approach` `using` `System;`   `class` `GFG` `{`   ` ``// Function to find maximum difference` `  ``// between the largest and smallest` `  ``// element of arr[] after K operations` `  ``static` `int` `maxDifference(``int``[] arr, ``int` `N, ``int` `K)` `  ``{` `    `  `    ``// Sort A in descending order` `    ``Array.Sort(arr);` `    ``Array.Reverse(arr);` `    `  `    ``// Case where no operation` `    ``// is to be performed` `    ``if` `(K == 0) {` `      ``return` `arr - arr[N - 1];` `    ``}` `    `  `    ``// Loop to iterate arr[]` `    ``for` `(``int` `i = 1; i < N && K != 0; ++i) {`   `      ``// Update arr` `      ``// and arr[i]` `      ``arr += arr[i];` `      ``arr[i] = 0;`   `      ``// Decrement K` `      ``K--;` `    ``}`   `    ``// Return Answer` `    ``return` `arr;` `  ``}`   `// Driver Code` `public` `static` `void` `Main()` `{` `    ``int``[] arr = { 3, 3, 2, 3, 3 };` `    ``int` `n = arr.Length;` `    ``int` `k = 1;`   `    ``Console.Write(maxDifference(arr, n, k)); ` `}` `}`   `// This code is contributed by sanjoy_62.`

## Javascript

 ``

Output

`6`

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!

Previous
Next