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++
// 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; } |
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.
Recommended Posts:
- Maximize array sum by X increments when each element is divided by 10
- Minimize subarray increments/decrements required to reduce all array elements to 0
- Length of Longest Subarray with same elements in atmost K increments
- Longest subarray whose elements can be made equal by maximum K increments
- Minimize count of increments of each element of subarrays required to make Array non-increasing
- Making elements distinct in a sorted array by minimum increments
- Minimum increments to convert to an array of consecutive integers
- Minimum increments by index value required to obtain at least two equal Array elements
- Minimum increments of Non-Decreasing Subarrays required to make Array Non-Decreasing
- Find K that requires minimum increments or decrements of array elements to obtain a sequence of increasing powers of K
- Maximize product of subarray sum with its minimum element
- Maximize the subarray sum after multiplying all elements of any subarray with X
- Find initial sequence that produces a given Array by cyclic increments upto index P
- Check if all elements of a Circular Array can be made equal by increments of adjacent pairs
- First subarray having sum at least half the maximum sum of any subarray of size K
- Maximum subarray size, such that all subarrays of that size have sum less than k
- Counting values greater than equal to x after increments
- Maximize the maximum subarray sum after removing atmost one element
- Maximize number of groups formed with size not smaller than its largest element
- Maximize count of subsets having product of smallest element and size of the subset at least X
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.

