Minimize the max of Array by doing at most K increment and decrement
Given a positive array arr[] of size N (2 ≤ N ≤ 105) and a positive integer K, the task is to minimize the maximum value of arr[] by performing at most K operations where in each operation, you can select any two distinct integers i and j (0 ≤ i, j < N), then increase the value of arr[i] by 1 and decrease the value of arr[j] by 1.
Example:
Input: arr = {3, 3, 7, 1, 6}, k = 2
Output: 6
Explanation:
One set of optimal operations is as follows:
For K = 1 => Select i = 2 and j = 0 and arr becomes {4, 3, 6, 1, 6}.
For K = 2 => Select i = 4 and j = 3, and arr becomes {4, 3, 6, 2, 5}.
The maximum integer of nums is . It can be shown that the maximum number cannot be less than 6.
Therefore, we return 6.Input: nums = {10, 1, 1}, k = 3
Output: 7
Approach: The problem can be solved using binary search based on the following idea.
Choose the minimum and maximum possible value in start and end variable respectively. Find the mid value of start and end. Check if we can choose mid as the maximum possible value in array. If its true then this is one of our valid answer. So update our answer in result and shift the start to mid – 1 to minimise the maximum possible value. Otherwise shift the end to mid + 1. Finally return the result.
Follow the steps below to implement the above idea:
- Initialise a variable start = 0, end = max value in arr[] and result
- While start is less than equal to end, do the following
- Calculate the mid by (start + end) / 2
- Check if mid can be a valid max Value by the following
- Initialize a variable operationCount = 0
- Iterate over the array arr[]
- Check if arr[i] is greater than our expected maximum is mid
- operationCount += mid – arr[i]
- Check if operationsCount is greater than K.
- If true, return false
- Check if arr[i] is greater than our expected maximum is mid
- Return true
- If mid is the valid maximum possible value then,
- Shift end to mid – 1
- store mid into result
- Otherwise, shift the start to mid + 1
- Return the result
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h> using namespace std; // Function to check if maxx can be a valid value bool isValid( int maxx, vector< int >& arr, int k) { // Initialize a variable sum = 0 long long operationCount = 0; // Iterate over the array arr[] for ( long long i = 0; i < arr.size(); i++) { // Keep storing the maximum value in sum if (arr[i] > maxx) operationCount += arr[i] - maxx; // Check if number of operation required to // make maximum element at max maxx. // If true, return false if (operationCount > k) return false ; } // Return true return true ; } // Function to find the minimum possible maximum int minimizeArrayMaxValue(vector< int >& arr, int k) { long long n = arr.size(); // Initialise a variable start = 0, // end = max value in, arr[] and result long long start = *min_element(arr.begin(), arr.end()), end = *max_element(arr.begin(), arr.end()); long long result; // Do while start is less than equals to end while (start <= end) { // Calculate the mid by (start + end) / 2; long long mid = (start + end) / 2; // Check if mid can be a valid max Value. if (isValid(mid, arr, k)) { // Shift end to mid - 1 end = mid - 1; // Store mid into result result = mid; cout<<result; } // Otherwise, shift start to mid + 1 else { start = mid + 1; } } // Return the result return result; } // Driver code int main() { vector< int > arr = { 3, 3, 7, 1, 6 }; int K = 3; // Function call cout << minimizeArrayMaxValue(arr, K); return 0; } |
Java
// java implementation import java.io.*; import java.util.ArrayList; import java.util.Collections; class GFG { // Function to check if maxx can be a valid value public static boolean isValid( int maxx, ArrayList<Integer> arr, int k) { // Initialize a variable sum = 0 int operationCount = 0 ; // Iterate over the array arr[] for ( int i = 0 ; i < arr.size(); i++) { // Keep storing the maximum value in sum if (arr.get(i) > maxx) operationCount += arr.get(i) - maxx; // Check if number of operation required to // make maximum element at max maxx. // If true, return false if (operationCount > k) return false ; } // Return true return true ; } // Function to find the minimum possible maximum public static int minimizeArrayMaxValue(ArrayList<Integer> arr, int k) { int n = arr.size(); // Initialise a variable start = 0, // end = max value in, arr[] and result int start = Collections.min(arr); int end = Collections.max(arr); int result = 0 ; // Do while start is less than equals to end while (start <= end) { // Calculate the mid by (start + end) / 2; int mid = ( int )((start + end) / 2 ); // Check if mid can be a valid max Value. if (isValid(mid, arr, k)) { // Shift end to mid - 1 end = mid - 1 ; // Store mid into result result = mid; } // Otherwise, shift start to mid + 1 else { start = mid + 1 ; } } // Return the result return result; } public static void main(String[] args) { int n = 5 ; ArrayList<Integer> arr = new ArrayList<Integer>(n); arr.add( 3 ); arr.add( 3 ); arr.add( 7 ); arr.add( 1 ); arr.add( 6 ); int K = 3 ; // Function call System.out.println(minimizeArrayMaxValue(arr, K)); } } // This code is contributed by ksam24000 |
Python3
# Function to check if maxx can be a valid value def isValid(maxx, arr, k): # Initialize a variable sum = 0 operationCount = 0 # Iterate over the array arr[] for i in range ( len (arr)): # Keep storing the maximum value in sum if arr[i] > maxx: operationCount + = arr[i] - maxx # Check if number of operation required to # make maximum element at max maxx. # If true, return false if operationCount > k: return False # Return true return True # Function to find the minimum possible maximum def minimizeArrayMaxValue(arr, k): # Initialise a variable start = 0, # end = max value in, arr[] and result start = min (arr) end = max (arr) result = 0 # Do while start is less than equals to end while start < = end: # Calculate the mid by (start + end) / 2 mid = (start + end) / / 2 # Check if mid can be a valid max Value. if isValid(mid, arr, k): # Shift end to mid - 1 end = mid - 1 # Store mid into result result = mid # Otherwise, shift start to mid + 1 else : start = mid + 1 # Return the result return result # Driver code arr = [ 3 , 3 , 7 , 1 , 6 ] K = 3 # Function call print (minimizeArrayMaxValue(arr, K)) # This code is contributed by Tapesh(tapeshdua420) |
C#
// Include namespace system using System; using System.Linq; using System.Collections.Generic; public class GFG { // Function to check if maxx can be a valid value public static bool isValid( int maxx, int [] arr, int k) { // Initialize a variable sum = 0 var operationCount = 0; // Iterate over the array arr[] for ( int i = 0; i < arr.Length; i++) { // Keep storing the maximum value in sum if (arr[i] > maxx) { operationCount += arr[i] - maxx; } // Check if number of operation required to // make maximum element at max maxx. // If true, return false if (operationCount > k) { return false ; } } // Return true return true ; } // Function to find the minimum possible maximum public static int minimizeArrayMaxValue( int [] arr, int k) { var n = arr.Length; // Initialise a variable start = 0, // end = max value in, arr[] and result var start = arr.Min(); var end = arr.Max(); var result = 0; // Do while start is less than equals to end while (start <= end) { // Calculate the mid by (start + end) / 2; var mid = ( int )(( int )((start + end) / 2)); // Check if mid can be a valid max Value. if (GFG.isValid(mid, arr, k)) { // Shift end to mid - 1 end = mid - 1; // Store mid into result result = mid; } else { start = mid + 1; } } // Return the result return result; } public static void Main(String[] args) { var arr = new int []{3,3,7,1,6}; var K = 3; // Function call Console.WriteLine(GFG.minimizeArrayMaxValue(arr, K)); } } // This code is contributed by aadityaburujwale. |
Javascript
// Include namespace system // JS implementation // Function to check if maxx can be a valid value function isValid(maxx, arr, k) { // Initialize a variable sum = 0 let operationCount = 0; // Iterate over the array arr[] for (let i = 0; i < arr.length; i++) { // Keep storing the maximum value in sum if (arr[i] > maxx) { operationCount += arr[i] - maxx; } // Check if number of operation required to // make maximum element at max maxx. // If true, return false if (operationCount > k) { return false ; } } // Return true return true ; } // Function to find the minimum possible maximum function minimizeArrayMaxValue(arr,k) { let n = arr.length; // Initialise a variable start = 0, // end = max value in, arr[] and result let start = Math.min.apply(Math, arr); let end = Math.max.apply(Math, arr); let result = 0; // Do while start is less than equals to end while (start <= end) { // Calculate the mid by (start + end) / 2; let mid = Math.floor((start + end) / 2); // Check if mid can be a valid max Value. if (isValid(mid, arr, k)) { // Shift end to mid - 1 end = mid - 1; // Store mid into result result = mid; } else { start = mid + 1; } } // Return the result return result; } let arr = [3,3,7,1,6]; let K = 3; // Function call console.log(minimizeArrayMaxValue(arr, K)); // This code is contributed by ksam24000 |
5
Time Complexity: O(N* log(max(arr)))
Auxiliary Space: O(1)
Related Articles:
Please Login to comment...