Given an array A[] consisting of N integers and an integer K, the task is to maximize the length of the subarray having equal elements after performing at most K increments by 1 on array elements.
Note: Same array element can be incremented more than once.
Examples:
Input: A[] = {2, 4, 8, 5, 9, 6}, K = 6
Output: 3
Explanation:
Subarray [8, 5, 9] can be modified to [9, 9, 9].
Total number of increments required is 5 which is less than K(= 6).
Input: A[] = {2, 2, 4}, K = 10
Output: 3
Naive Approach: The simplest approach to solve the problem is to generate all possible subarrays and for each subarray, check if all its elements can be made equal in at most K increments. Print the maximum length of such subarrays obtained.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Approach: The above approach can be optimized using the Sliding Window technique. Follow the steps below to solve the problem:
- Keep a track of the maximum element of the window.
- The total operations required for a particular window is obtained by the following equation:
Count of operations = (Length of the window calculated so far + 1) * (Maximum element from the window) – Sum of the window
- Now, check if the above-calculated value exceeds K or not. If so, then slide the starting pointer of the window towards the right, otherwise increment the length of the window calculated so far.
- Repeat the above steps to obtain the longest window satisfying the required condition.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
#define newl "\n"
int maxSubarray( int a[], int k, int n)
{
int answer = 0;
int start = 0;
long int s = 0;
deque< int > dq;
for ( int i = 0; i < n; i++)
{
int x = a[i];
while (!dq.empty() &&
a[dq.front()] <= x)
dq.pop_front();
dq.push_back(i);
s += x;
long int cost = ( long int )a[dq.front()] *
(answer + 1) - s;
if (cost <= ( long int )k)
answer++;
else
{
if (dq.front() == start)
dq.pop_front();
s -= a[start++];
}
}
return answer;
}
int main()
{
int a[] = { 2, 2, 4 };
int k = 10;
int n = sizeof (a) / sizeof (a[0]);
cout << (maxSubarray(a, k, n));
return 0;
}
|
Java
import java.util.*;
class GFG {
static int maxSubarray( int [] a, int k)
{
int n = a.length;
int answer = 0 ;
int start = 0 ;
long s = 0 ;
Deque<Integer> dq = new LinkedList<>();
for ( int i = 0 ; i < n; i++) {
int x = a[i];
while (!dq.isEmpty() && a[dq.peek()] <= x)
dq.poll();
dq.add(i);
s += x;
long cost
= ( long )a[dq.peekFirst()] * (answer + 1 )
- s;
if (cost <= ( long )k)
answer++;
else {
if (dq.peekFirst() == start)
dq.pollFirst();
s -= a[start++];
}
}
return answer;
}
public static void main(String[] args)
{
int [] a = { 2 , 2 , 4 };
int k = 10 ;
System.out.println(maxSubarray(a, k));
}
}
|
Python3
from collections import deque
def maxSubarray(a, k):
n = len (a)
answer = 0
start = 0
s = 0
dq = deque()
for i in range (n):
x = a[i]
while ( len (dq) > 0 and a[dq[ - 1 ]] < = x):
dq.popleft()
dq.append(i)
s + = x
cost = a[dq[ 0 ]] * (answer + 1 ) - s
if (cost < = k):
answer + = 1
else :
if (dq[ 0 ] = = start):
dq.popleft()
s - = a[start]
start + = 1
return answer
if __name__ = = '__main__' :
a = [ 2 , 2 , 4 ]
k = 10
print (maxSubarray(a, k))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int maxSubarray( int [] a, int k)
{
int n = a.Length;
int answer = 0;
int start = 0;
long s = 0;
Queue< int > dq = new Queue< int >();
for ( int i = 0; i < n; i++)
{
int x = a[i];
while (dq.Count!=0 &&
a[dq.Peek()] <= x)
dq.Dequeue();
dq.Enqueue(i);
s += x;
long cost = ( long )a[dq.Peek()] *
(answer + 1) - s;
if (cost <= ( long )k)
answer++;
else
{
if (dq.Peek() == start)
dq.Dequeue();
s -= a[start++];
}
}
return answer;
}
public static void Main(String[] args)
{
int [] a = {2, 2, 4};
int k = 10;
Console.WriteLine(maxSubarray(a, k));
}
}
|
Javascript
<script>
function maxSubarray(a, k, n)
{
var answer = 0;
var start = 0;
var s = 0;
var dq = [];
for ( var i = 0; i < n; i++)
{
var x = a[i];
while (dq.length!=0 &&
a[dq[0]] <= x)
dq.shift();
dq.push(i);
s += x;
var cost = a[dq[0]] *
(answer + 1) - s;
if (cost <= k)
answer++;
else
{
if (dq[0] == start)
dq.shift();
s -= a[start++];
}
}
return answer;
}
var a = [2, 2, 4];
var k = 10;
var n = a.length;
document.write(maxSubarray(a, k, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
20 May, 2021
Like Article
Save Article