Maximize the minimum array element by M subarray increments of size S

Given an array arr[] of N integers and two integers S and M, the task is to maximize the minimum array element by incrementing any subarray of size S by 1, M number of times.

Examples:

Input: arr[] = {1, 2, 3, 4, 5, 6}, S = 2, M = 3
Output: 3
Explanation:
Below are the operations performed:
Operation 1: Select subarray {1, 2} and after increment, array arr[] becomes = {2, 3, 3, 4, 5, 6}.
Operation 2: Select subarray {2, 3} and after increment, array arr[] becomes = {3, 4, 3, 4, 5, 6}.
Operation 3: Select subarray {3, 4} and after increment, array arr[] becomes = {4, 5, 3, 4, 5, 6}.
After the above operations, the minimum element of the array is 3.

Input: arr[] = {3, 5, 2, 7, 3}, S = 3, M = 3
Output: 4
Explanation:
Below are the operations performed:
Operation 1: Select subarray {3, 5, 2} and after increment, array arr[] becomes = {4, 6, 3, 7, 3}.
Operation 2: Select subarray {4, 6, 3} and after increment, array arr[] becomes = {5, 7, 4, 7, 3}.
Operation 3: Select subarray {4, 7, 3} and after increment, array arr[] becomes = {5, 7, 5, 8, 4}.
After the above operations, the minimum element of the array is 4.

Approach: The idea is to find the minimum element of the array M number of times and increment subarrays of size S from that minimum element by 1. Follow the steps below to solve the problem:



  • Traverse over the array M number of times and for each iteration do the following:
    • Find the minimum element of the array arr[]. Let the first index of the minimum element be idx.
    • Increment the current minimum element by 1.
    • Now take two pointers leftIdx as idx – 1 and rightIdx as idx + 1.
    • If the element at leftIdx is less than the element at rightIdx then increment A[leftIndex] by 1 and decrement leftIndex by 1. Otherwise, increment A[rightIndex] by 1 and rightIndex by 1. Continue this step till (S – 1) elements are processed.
  • After the above iterations, print the minimum element of the updated array.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return index of minimum
// element in the array
int min(int a[], int n)
{
    // Initialize a[0] as minValue
    int minIndex = 0, minValue = a[0], i;
  
    // Traverse the array
    for (i = 1; i < n; i++) {
  
        // If a[i] < existing minValue
        if (a[i] < minValue) {
            minValue = a[i];
            minIndex = i;
        }
    }
  
    // Return the minimum index
    return minIndex;
}
  
// Function that maximize the minimum
// element of array after incrementing
// subarray of size S by 1, M times
int maximizeMin(int A[], int N,
                int S, int M)
{
    int minIndex, left, right, i, j;
  
    // Iterating through the array
    // for M times
    for (i = 0; i < M; i++) {
  
        // Find minimum element index
        minIndex = min(A, N);
  
        // Increment the minimum value
        A[minIndex]++;
  
        // Storing the left index
        // and right index
        left = minIndex - 1;
        right = minIndex + 1;
  
        // Incrementing S - 1 minumum
        // elements to the left and
        // right of minValue
        for (j = 0; j < S - 1; j++) {
  
            // Reached extreme left
            if (left == -1)
                A[right++]++;
  
            // Reached extreme right
            else if (right == -1)
                A[left--]++;
  
            else {
  
                // Left value is minimum
                if (A[left] < A[right])
                    A[left--]++;
  
                // Right value is minimum
                else
                    A[right++]++;
            }
        }
    }
  
    // Find the minValue in A[] after
    // M operations
    minIndex = min(A, N);
  
    // Return the minimum value
    return A[minIndex];
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int S = 2, M = 3;
  
    // Function Call
    cout << maximizeMin(arr, N, S, M);
  
    return 0;
}

chevron_right


Output:

3

Time Complexity: O(M*N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.