Maximize the minimum value of Array by performing given operations at most K times
Last Updated :
04 Dec, 2023
Given array A[] of size N and integer K, the task for this problem is to maximize the minimum value of the array by performing given operations at most K times. In one operation choose any index and increase that array element by 1.
Examples:
Input: A[] = {3, 1, 2, 4, 6, 2, 5}, K = 8
Output: 4
Explanation:
- Choose i = 1 and increase the element by 1 now A[] becomes {4, 1, 2, 4, 6, 2, 5}
- Choose i = 2 and increase the element by 1 now A[] becomes {4, 2, 2, 4, 6, 2, 5}
- Choose i = 2 and increase the element by 1 now A[] becomes {4, 3, 2, 4, 6, 2, 5}
- Choose i = 2 and increase the element by 1 now A[] becomes {4, 4, 2, 4, 6, 2, 5}
- Choose i = 3 and increase the element by 1 now A[] becomes {4, 4, 3, 4, 6, 2, 5}
- Choose i = 3 and increase the element by 1 now A[] becomes {4, 4, 4, 4, 6, 2, 5}
- Choose i = 6 and increase the element by 1 now A[] becomes {4, 4, 4, 4, 6, 3, 5}
- Choose i = 6 and increase the element by 1 now A[] becomes {4, 4, 4, 4, 6, 4, 5}
After all K = 8 operations minimum value of the array is 4.
Input: A[] = {1, 1, 1}, K = 10
Output: 4
Explanation:
- Choose i = 1 and increase the element by 1 now A[] becomes {2, 1, 1}
- Choose i = 1 and increase the element by 1 now A[] becomes {3, 1, 1}
- Choose i = 1 and increase the element by 1 now A[] becomes {4, 1, 1}
- Choose i = 2 and increase the element by 1 now A[] becomes {4, 2, 1}
- Choose i = 2 and increase the element by 1 now A[] becomes {4, 3, 1}
- Choose i = 2 and increase the element by 1 now A[] becomes {4, 4, 1}
- Choose i = 3 and increase the element by 1 now A[] becomes {4, 4, 2}
- Choose i = 3 and increase the element by 1 now A[] becomes {4, 4, 3}
- Choose i = 3 and increase the element by 1 now A[] becomes {4, 4, 4}
- Choose i = 3 and increase the element by 1 now A[] becomes {4, 4, 5}
After all K = 10 operations minimum value of array is 4
Naïve Approach: The basic way to solve the problem is as follows:
Check For every value from 1 to 109 is possible or not. This can be done for each value it is possible to make every element at least equal to greater than number that we are checking for.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To solve the problem follow the below idea:
Binary Search can be used to solve this problem and the range of binary search will be 1 to 109. f(N) is monotonic function represents whether every element of array can be made at least N by at most K operations. it is of the form TTTTTTFFFFFFF. we have to find when the last time function was true using Binary Search.
Below are the steps for the above approach:
- Set the low and high ranges of binary search.
- isPossible(mid) function used to check whether every element of array A[] can be made at least mid by performing given operations at most K number of times.
- Run while loop till high and low are not equal.
- In the while loop find the middle element and store it in a mid variable.
- Check if every array element of array A[] can be made at least mid in at most K operations using the isPossible() function. If it is true, set low = mid else high = mid – 1.
- After the loop ends if isPossible() is possibly true for high then return high else return low.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
bool isPossible( int mid, int A[], int N, int K)
{
int reqOperations = 0;
for ( int i = 0; i < N; i++) {
if (A[i] < mid)
reqOperations += (mid - A[i]);
}
return reqOperations <= K;
}
int maximizeMinimumElement( int A[], int N, int K)
{
int low = 1, high = 3000000000;
while (high - low > 1) {
int mid = (low + high) / 2;
if (isPossible(mid, A, N, K)) {
low = mid;
}
else {
high = mid - 1;
}
}
if (isPossible(high, A, N, K))
return high;
else
return low;
}
int32_t main()
{
int K = 8;
int A[] = { 3, 1, 2, 4, 6, 2, 5 }, N = 7;
cout << maximizeMinimumElement(A, N, K) << endl;
return 0;
}
|
Java
class GFG {
static boolean IsPossible( long mid, int [] A, int N,
int K)
{
long reqOperations = 0 ;
for ( int i = 0 ; i < N; i++) {
if (A[i] < mid)
reqOperations += (mid - A[i]);
}
return reqOperations <= K;
}
static long MaximizeMinimumElement( int [] A, int N,
int K)
{
long low = 1 , high = 3000000000L;
while (high - low > 1 ) {
long mid = (low + high) / 2 ;
if (IsPossible(mid, A, N, K)) {
low = mid;
}
else {
high = mid - 1 ;
}
}
if (IsPossible(high, A, N, K))
return high;
else
return low;
}
public static void main(String[] args)
{
int K = 8 ;
int [] A = { 3 , 1 , 2 , 4 , 6 , 2 , 5 };
int N = 7 ;
System.out.println(MaximizeMinimumElement(A, N, K));
}
}
|
Python3
def isPossible(mid, A, N, K):
reqOperations = 0
for i in range (N):
if A[i] < mid:
reqOperations + = (mid - A[i])
return reqOperations < = K
def maximizeMinimumElement(A, N, K):
low = 1
high = 3000000000
while high - low > 1 :
mid = (low + high) / / 2
if isPossible(mid, A, N, K):
low = mid
else :
high = mid - 1
if isPossible(high, A, N, K):
return high
else :
return low
K = 8
A = [ 3 , 1 , 2 , 4 , 6 , 2 , 5 ]
N = len (A)
print (maximizeMinimumElement(A, N, K))
|
C#
using System;
class GFG
{
static bool IsPossible( long mid, int [] A, int N, int K)
{
long reqOperations = 0;
for ( int i = 0; i < N; i++)
{
if (A[i] < mid)
reqOperations += (mid - A[i]);
}
return reqOperations <= K;
}
static long MaximizeMinimumElement( int [] A, int N, int K)
{
long low = 1, high = 3000000000;
while (high - low > 1)
{
long mid = (low + high) / 2;
if (IsPossible(mid, A, N, K))
{
low = mid;
}
else
{
high = mid - 1;
}
}
if (IsPossible(high, A, N, K))
return high;
else
return low;
}
static void Main()
{
int K = 8;
int [] A = { 3, 1, 2, 4, 6, 2, 5 };
int N = 7;
Console.WriteLine(MaximizeMinimumElement(A, N, K));
}
}
|
Javascript
function isPossible(mid, A, N, K) {
let reqOperations = 0;
for (let i = 0; i < N; i++) {
if (A[i] < mid) {
reqOperations += (mid - A[i]);
}
}
return reqOperations <= K;
}
function maximizeMinimumElement(A, N, K) {
let low = 1;
let high = 3000000000;
while (high - low > 1) {
let mid = Math.floor((low + high) / 2);
if (isPossible(mid, A, N, K)) {
low = mid;
} else {
high = mid - 1;
}
}
if (isPossible(high, A, N, K)) {
return high;
}
return low;
}
const K = 8;
const A = [3, 1, 2, 4, 6, 2, 5];
const N = 7;
console.log(maximizeMinimumElement(A, N, K));
|
Time Complexity: O(N*log(10^9))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...