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++ code of the above approach #include <bits/stdc++.h> using namespace std;
// Function to check whether the mid is // smallest minimum of array 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;
}
}
// If it is true
return (ans <= k);
} // Function to maximize the minimum height 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;
// Check for mid
if (isvalid(a, n, k, w, mid)) {
l = mid + 1;
ans = mid;
}
else {
h = mid - 1;
}
}
// Return the minimum height
return ans;
} // Driver code int main()
{ int N = 6, K = 2, W = 3;
vector< int > a = { 2, 2, 2, 2, 1, 1 };
// Function call
int ans = maximizeMinHeight(N, K, W, a);
cout << ans;
return 0;
} |
import java.util.*;
public class Main {
// Function to check whether the mid is
// smallest minimum opf array
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;
}
}
// If it is true
return (ans <= k);
}
// Function to maximize the minimum height
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 ;
// Check for mid
if (isvalid(a, n, k, w, mid)) {
l = mid + 1 ;
ans = ( int )mid;
}
else {
h = mid - 1 ;
}
}
// Return the minimum height
return ans;
}
// Driver code
public static void main(String[] args)
{
int N = 6 , K = 2 , W = 3 ;
int [] a = { 2 , 2 , 2 , 2 , 1 , 1 };
// Function call
long ans = maximizeMinHeight(N, K, W, a);
System.out.println(ans);
}
} |
def isvalid(a, n, k, w, mid):
"""
Function to check whether the mid is the smallest minimum of the array
"""
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
# If it is true
return ans < = k
def maximizeMinHeight(n, k, w, a):
"""
Function to maximize the minimum height
"""
ans = 0
l, h = 0 , 10 * * 14
while l < = h:
mid = (l + h) / / 2
# Check for mid
if isvalid(a, n, k, w, mid):
l = mid + 1
ans = mid
else :
h = mid - 1
# Return the minimum height
return ans
if __name__ = = '__main__' :
N, K, W = 6 , 2 , 3
a = [ 2 , 2 , 2 , 2 , 1 , 1 ]
# Function call
ans = maximizeMinHeight(N, K, W, a)
print (ans)
|
// C# code of the above approach using System;
public class GFG {
// Function to check whether the mid is
// smallest minimum opf array
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;
}
}
// If it is true
return (ans <= k);
}
// Function to maximize the minimum height
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);
// Check for mid
if (isvalid(a, n, k, w, mid)) {
l = mid + 1;
ans = mid;
}
else {
h = mid - 1;
}
}
// Return the minimum height
return ans;
}
// Driver code
public static void Main() {
int N = 6, K = 2, W = 3;
int [] a = { 2, 2, 2, 2, 1, 1 };
// Function call
long ans = maximizeMinHeight(N, K, W, a);
Console.WriteLine(ans);
}
} |
// Function to check whether the mid is // smallest minimum of array 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;
} } // If it is true return ans <= k;
} // Function to maximize the minimum height function maximizeMinHeight(n, k, w, a) {
let ans = 0; let l = 0, h = 1e14; while (l <= h) {
let mid = Math.floor((l + h) / 2); // Check for mid if (isvalid(a, n, k, w, mid)) {
l = mid + 1;
ans = mid;
} else {
h = mid - 1;
} } // Return the minimum height return ans;
} // Driver code let N = 6, K = 2, W = 3; let a = [2, 2, 2, 2, 1, 1]; // Function call let ans = maximizeMinHeight(N, K, W, a); console.log(ans); |
2
Time Complexity: O(N*LogN)
Auxiliary Space: O(N)