Maximize the minimum array element by M subarray increments of size S
Last Updated :
13 Sep, 2021
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++
#include <bits/stdc++.h>
using namespace std;
int min( int a[], int n)
{
int minIndex = 0, minValue = a[0], i;
for (i = 1; i < n; i++) {
if (a[i] < minValue) {
minValue = a[i];
minIndex = i;
}
}
return minIndex;
}
int maximizeMin( int A[], int N,
int S, int M)
{
int minIndex, left, right, i, j;
for (i = 0; i < M; i++) {
minIndex = min(A, N);
A[minIndex]++;
left = minIndex - 1;
right = minIndex + 1;
for (j = 0; j < S - 1; j++) {
if (left == -1)
A[right++]++;
else if (right == N)
A[left--]++;
else {
if (A[left] < A[right])
A[left--]++;
else
A[right++]++;
}
}
}
minIndex = min(A, N);
return A[minIndex];
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
int S = 2, M = 3;
cout << maximizeMin(arr, N, S, M);
return 0;
}
|
Java
import java.util.*;
class solution{
static int min1( int a[], int n)
{
int minIndex = 0 ,
minValue = a[ 0 ], i;
for (i = 1 ; i < n; i++)
{
if (a[i] < minValue)
{
minValue = a[i];
minIndex = i;
}
}
return minIndex;
}
static int maximizeMin( int A[], int N,
int S, int M)
{
int minIndex, left, right, i, j;
for (i = 0 ; i < M; i++)
{
minIndex = min1(A, N);
A[minIndex]++;
left = minIndex - 1 ;
right = minIndex + 1 ;
for (j = 0 ; j < S - 1 ; j++)
{
if (left == - 1 )
A[right++]++;
else if (right == N)
A[left--]++;
else
{
if (A[left] < A[right])
A[left--]++;
else
A[right++]++;
}
}
}
minIndex = min1(A, N);
return A[minIndex];
}
public static void main(String args[])
{
int []arr = { 1 , 2 , 3 ,
4 , 5 , 6 };
int N = arr.length;
int S = 2 , M = 3 ;
System.out.print(maximizeMin(arr, N, S, M));
}
}
|
Python3
def min (a, n):
minIndex = 0
minValue = a[ 0 ]
for i in range ( 1 , n):
if (a[i] < minValue):
minValue = a[i]
minIndex = i
return minIndex
def maximizeMin(A, N, S, M):
minIndex, left, right = 0 , 0 , 0
for i in range (M):
minIndex = min (A, N)
A[minIndex] + = 1
left = minIndex - 1
right = minIndex + 1
for j in range (S - 1 ):
if (left = = - 1 ):
A[right] + = 1
right + = 1
elif (right = = N):
A[left] + = 1
left - = 1
else :
if (A[left] < A[right]):
A[left] + = 1
left - = 1
else :
A[right] + = 1
right + = 1
minIndex = min (A, N)
return A[minIndex]
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
N = len (arr)
S = 2
M = 3
print (maximizeMin(arr, N, S, M))
|
C#
using System;
class GFG{
static int min1( int [] a,
int n)
{
int minIndex = 0,
minValue = a[0], i;
for (i = 1; i < n; i++)
{
if (a[i] < minValue)
{
minValue = a[i];
minIndex = i;
}
}
return minIndex;
}
static int maximizeMin( int [] A, int N,
int S, int M)
{
int minIndex, left, right, i, j;
for (i = 0; i < M; i++)
{
minIndex = min1(A, N);
A[minIndex]++;
left = minIndex - 1;
right = minIndex + 1;
for (j = 0; j < S - 1; j++)
{
if (left == -1)
A[right++]++;
else if (right == N)
A[left--]++;
else
{
if (A[left] < A[right])
A[left--]++;
else
A[right++]++;
}
}
}
minIndex = min1(A, N);
return A[minIndex];
}
static void Main()
{
int [] arr = {1, 2, 3,
4, 5, 6};
int N = arr.Length;
int S = 2, M = 3;
Console.Write(maximizeMin(arr, N,
S, M));
}
}
|
Javascript
<script>
function min1(a, n)
{
let minIndex = 0,
minValue = a[0], i;
for (i = 1; i < n; i++)
{
if (a[i] < minValue)
{
minValue = a[i];
minIndex = i;
}
}
return minIndex;
}
function maximizeMin(A, N, S, M)
{
let minIndex, left, right, i, j;
for (i = 0; i < M; i++)
{
minIndex = min1(A, N);
A[minIndex]++;
left = minIndex - 1;
right = minIndex + 1;
for (j = 0; j < S - 1; j++)
{
if (left == -1)
A[right++]++;
else if (right == N)
A[left--]++;
else
{
if (A[left] < A[right])
A[left--]++;
else
A[right++]++;
}
}
}
minIndex = min1(A, N);
return A[minIndex];
}
let arr = [1, 2, 3,
4, 5, 6];
let N = arr.length;
let S = 2, M = 3;
document.write(maximizeMin(arr, N, S, M));
</script>
|
Time Complexity: O(M*N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...