Maximize the minimum element and return it
Last Updated :
28 Apr, 2023
Given an array A[] of size N along with W, K. We can increase W continuous elements by 1 where we are allowed to perform this operation K times, the task is to maximize the minimum element and return the minimum element after operations.
Examples:
Input: N = 6, K = 2, W = 3, A[] = {2, 2, 2, 2, 1, 1}
Output: 2
Explanation: Increase the last three elements for the first time & first three elements for the second time. The new elements will be {3, 3, 3, 3, 2, 2}
Input: N = 2, K = 5, W = 1, A[] = {5, 8}
Output: 9
Explanation: Four times increase the first element then increase the last flower once.
Approach: This can be solved with the following idea:
The idea is to use a binary search between the minimum and maximum possible element and at every iteration of the binary search check if we can get the minimum of the element equal to the mid value of the binary search by increasing the element k times.
Steps involved in the implementation of code:
- Initially take the answer as 0 and do a binary search between the minimum and maximum possible height of the tree, i.e: l = 0 and h = 10^14.
- Run a loop while(l ≤ h) for binary searching.
- Find mid = (l + h) / 2
- Now check if this mid value is one of the possible values for our answer or not, if it is possible to get the minimum of the array to be equal to mid then update the answer as mid and l = mid + 1.
- To check whether this mid value is valid or not we call another function isValid this function implementation is as follows:
- Create an array ps which keeps track of which element is increased by how many values and initialize it with zero.
- Now run a loop and check if the initial element + increased element( a[i] + ps[i] ) is less than the mid then incr the ith height with the required value so that its height is equal to mid, and along with that we also increase the value the next w elements as we can increase the value of w element in one time.
- When coming out of the loop check if k is greater than or equal to we increased the value of the element true else return false
- Else if this mid value is not a possible value then updates h = mid-1.
- Return answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isvalid(vector< int >& a, int n, int k, int w,
long long int mid)
{
long long int ps[n];
fill(ps, ps + n, 0ll);
long long int ans = 0;
for ( long long int i = 0; i < n; i++) {
if (i - 1 >= 0)
ps[i] += ps[i - 1];
if (a[i] + ps[i] < mid) {
long long int e = mid - a[i] - ps[i];
ans += e;
ps[i] += e;
if (i + w < n)
ps[i + w] -= e;
}
}
return (ans <= k);
}
long long int maximizeMinHeight( int n, int k, int w,
vector< int >& a)
{
int ans = 0;
long long int l = 0, h = 1e14;
while (l <= h) {
long long int mid = (l + h) / 2;
if (isvalid(a, n, k, w, mid)) {
l = mid + 1;
ans = mid;
}
else {
h = mid - 1;
}
}
return ans;
}
int main()
{
int N = 6, K = 2, W = 3;
vector< int > a = { 2, 2, 2, 2, 1, 1 };
int ans = maximizeMinHeight(N, K, W, a);
cout << ans;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static boolean isvalid( int [] a, int n, int k,
int w, long mid)
{
long [] ps = new long [n];
Arrays.fill(ps, 0 );
long ans = 0 ;
for ( long i = 0 ; i < n; i++) {
if (i - 1 >= 0 )
ps[( int )i] += ps[( int )i - 1 ];
if (a[( int )i] + ps[( int )i] < mid) {
long e = mid - a[( int )i] - ps[( int )i];
ans += e;
ps[( int )i] += e;
if (i + w < n)
ps[( int )i + w] -= e;
}
}
return (ans <= k);
}
public static long maximizeMinHeight( int n, int k,
int w, int [] a)
{
int ans = 0 ;
long l = 0 , h = ( long )1e14;
while (l <= h) {
long mid = (l + h) / 2 ;
if (isvalid(a, n, k, w, mid)) {
l = mid + 1 ;
ans = ( int )mid;
}
else {
h = mid - 1 ;
}
}
return ans;
}
public static void main(String[] args)
{
int N = 6 , K = 2 , W = 3 ;
int [] a = { 2 , 2 , 2 , 2 , 1 , 1 };
long ans = maximizeMinHeight(N, K, W, a);
System.out.println(ans);
}
}
|
Python3
def isvalid(a, n, k, w, mid):
ps = [ 0 ] * n
ans = 0
for i in range (n):
if i - 1 > = 0 :
ps[i] + = ps[i - 1 ]
if a[i] + ps[i] < mid:
e = mid - a[i] - ps[i]
ans + = e
ps[i] + = e
if i + w < n:
ps[i + w] - = e
return ans < = k
def maximizeMinHeight(n, k, w, a):
ans = 0
l, h = 0 , 10 * * 14
while l < = h:
mid = (l + h) / / 2
if isvalid(a, n, k, w, mid):
l = mid + 1
ans = mid
else :
h = mid - 1
return ans
if __name__ = = '__main__' :
N, K, W = 6 , 2 , 3
a = [ 2 , 2 , 2 , 2 , 1 , 1 ]
ans = maximizeMinHeight(N, K, W, a)
print (ans)
|
C#
using System;
public class GFG {
public static bool isvalid( int [] a, int n, int k, int w,
long mid)
{
long [] ps = new long [n];
Array.Fill(ps, 0L);
long ans = 0;
for ( long i = 0; i < n; i++) {
if (i - 1 >= 0)
ps[i] += ps[i - 1];
if (a[i] + ps[i] < mid) {
long e = mid - a[i] - ps[i];
ans += e;
ps[i] += e;
if (i + w < n)
ps[i + w] -= e;
}
}
return (ans <= k);
}
public static long maximizeMinHeight( int n, int k,
int w, int [] a)
{
long ans = 0;
long l = 0, h = ( long )1e14;
while (l <= h) {
long mid = ((l+ h) / 2);
if (isvalid(a, n, k, w, mid)) {
l = mid + 1;
ans = mid;
}
else {
h = mid - 1;
}
}
return ans;
}
public static void Main() {
int N = 6, K = 2, W = 3;
int [] a = { 2, 2, 2, 2, 1, 1 };
long ans = maximizeMinHeight(N, K, W, a);
Console.WriteLine(ans);
}
}
|
Javascript
function isvalid(a, n, k, w, mid) {
let ps = new Array(n).fill(0);
let ans = 0;
for (let i = 0; i < n; i++) {
if (i - 1 >= 0) ps[i] += ps[i - 1];
if (a[i] + ps[i] < mid) {
let e = mid - a[i] - ps[i];
ans += e;
ps[i] += e;
if (i + w < n) ps[i + w] -= e;
}
}
return ans <= k;
}
function maximizeMinHeight(n, k, w, a) {
let ans = 0;
let l = 0,
h = 1e14;
while (l <= h) {
let mid = Math.floor((l + h) / 2);
if (isvalid(a, n, k, w, mid)) {
l = mid + 1;
ans = mid;
} else {
h = mid - 1;
}
}
return ans;
}
let N = 6,
K = 2,
W = 3;
let a = [2, 2, 2, 2, 1, 1];
let ans = maximizeMinHeight(N, K, W, a);
console.log(ans);
|
Time Complexity: O(N*LogN)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...