Maximize the smallest array element by incrementing all elements in a K-length subarray by 1 exactly M times
Last Updated :
13 Aug, 2021
Given an array arr[] of size N, and integers M and K, the task is to find the maximum possible value of the smallest array element by performing M operations. In each operation, increase the value of all elements in a contiguous subarray of length K by 1.
Examples:
Input: arr[ ] = {2, 2, 2, 2, 1, 1}, M = 1, K = 3
Output: 2
Explanation: Update the last 3 elements on the first move then updated array is [2, 2, 2, 3, 2, 2]. The smallest element has a value of 2.
Input: arr[ ] = {5, 8}, M = 5, K = 1
Output: 9
Approach: The problem can be solved by using Binary Search. Traverse the array arr[] and for every element arr[i], count the number of operations required. If the current element is required to be updated x times, then add x to the answer and update the consecutive segment of length K by x times.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n, m, k, l, r, i;
ll check(ll v, vector<ll>& a)
{
ll tec = 0, ans = 0;
vector<ll> b(n + k + 1);
for (i = 0; i < n; i++) {
tec -= b[i];
if (a[i] + tec < v) {
ll mov = v - a[i] - tec;
ans = ans + mov;
tec += mov;
b[i + k] = mov;
}
}
return (ans <= m);
}
ll FindLargest(vector<ll> a)
{
l = 1;
r = pow (10, 10);
while (r - l > 0) {
ll tm = (l + r + 1) / 2;
if (check( tm , a))
l = tm ;
else
r = tm - 1;
}
return l;
}
int main()
{
vector<ll> a{ 2, 2, 2, 2, 1, 1 };
m = 2;
k = 3;
n = a.size();
cout << FindLargest(a);
return 0;
}
|
Java
class GFG{
static long n, m, k, l, r, i;
static boolean check( long v, long [] a)
{
long tec = 0 , ans = 0 ;
long [] b = new long [( int )(n + k + 1 )];
for ( int i = 0 ; i < n; i++)
{
tec -= b[i];
if (a[i] + tec < v)
{
long mov = v - a[i] - tec;
ans = ans + mov;
tec += mov;
b[i + ( int )k] = mov;
}
}
return ans <= m;
}
static long FindLargest( long [] a)
{
l = 1 ;
r = ( long )Math.pow( 10 , 10 );
while (r - l > 0 )
{
long tm = (l + r + 1 ) / 2 ;
if (check(tm, a))
l = tm;
else
r = tm - 1 ;
}
return l;
}
public static void main(String[] args)
{
long [] a = { 2 , 2 , 2 , 2 , 1 , 1 };
m = 2 ;
k = 3 ;
n = a.length;
System.out.println(FindLargest(a));
}
}
|
Python3
n = 0
m = 0
k = 0
l = 0
r = 0
i = 0
from math import pow
def check(v, a):
tec = 0
ans = 0
b = [ 0 for i in range (n + k + 1 )]
for i in range (n):
tec - = b[i]
if (a[i] + tec < v):
mov = v - a[i] - tec
ans = ans + mov
tec + = mov
b[i + k] = mov
return (ans < = m)
def FindLargest(a):
l = 1
r = pow ( 10 , 10 )
while (r - l > 0 ):
tm = (l + r + 1 ) / / 2
if (check(tm, a)):
l = tm
else :
r = tm - 1
return l
if __name__ = = '__main__' :
a = [ 2 , 2 , 2 , 2 , 1 , 1 ]
m = 2
k = 3
n = len (a)
print ( int (FindLargest(a)))
|
C#
using System;
public class GFG
{
static long n, m, k, l, r, i;
static bool check( long v, long [] a)
{
long tec = 0, ans = 0;
long [] b = new long [( int )(n + k + 1)];
for ( int i = 0; i < n; i++)
{
tec -= b[i];
if (a[i] + tec < v)
{
long mov = v - a[i] - tec;
ans = ans + mov;
tec += mov;
b[i + ( int )k] = mov;
}
}
return ans <= m;
}
static long FindLargest( long [] a)
{
l = 1;
r = ( long )Math.Pow(10, 10);
while (r - l > 0)
{
long tm = (l + r + 1) / 2;
if (check(tm, a))
l = tm;
else
r = tm - 1;
}
return l;
}
public static void Main(String[] args)
{
long [] a = { 2, 2, 2, 2, 1, 1 };
m = 2;
k = 3;
n = a.Length;
Console.WriteLine(FindLargest(a));
}
}
|
Javascript
<script>
let n = 0, m = 0, k = 0, l = 0, r = 0, i = 0;
function check(v, a)
{
let tec = 0,
ans = 0;
let b = new Array(n + k + 1).fill(0);
for (i = 0; i < n; i++)
{
tec -= b[i];
if (a[i] + tec < v)
{
let mov = v - a[i] - tec;
ans = ans + mov;
tec += mov;
b[i + k] = mov;
}
}
return ans <= m;
}
function FindLargest(a)
{
l = 1;
r = Math.pow(10, 10);
while (r - l > 0)
{
let tm = Math.floor((l + r + 1) / 2);
if (check(tm, a)) l = tm;
else r = tm - 1;
}
return l;
}
let a = [ 2, 2, 2, 2, 1, 1 ];
m = 2;
k = 3;
n = a.length;
document.write(FindLargest(a));
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(N + K)
Share your thoughts in the comments
Please Login to comment...