Open In App

Maximize the minimum element and return it

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:

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);

Output
2

Time Complexity: O(N*LogN)
Auxiliary Space: O(N)


Article Tags :