Minimum number of operations to make maximum element of every subarray of size K at least X
Last Updated :
29 Jan, 2024
Given an array A[] of size N and an integer K, find minimum number of operations to make maximum element of every subarray of size K at least X. In one operation, we can increment any element of the array by 1.
Examples:
Input: A[] = {2, 3, 0, 0, 2}, K = 3, X =4
Output: 3
Explanation: Perform following operation to make every subarray of size 3 at least X = 4
- Choose index i = 1 (A[1] = 3) and increment by 1, A[1] becomes 4.
- Choose index i = 4 (A[4] = 2) and increment by 1, A[4] becomes 3.
- Choose index i = 4 (A[4] = 3) and increment by 1, A[4] becomes 4.
After 3 operations array A[] becomes {2, 4, 0, 0, 4} whose every subarray of size 3 is at least X = 4
Input: A[] = {0, 1, 3, 3}, K = 3, X = 5
Output: 2
Explanation: Perform following operation to make every subarray of size 3 at least X = 5
- Choose index i = 2 (A[2] = 3) and increment by 1, A[2] becomes 4.
- Choose index i = 2 (A[2] = 3) and increment by 1, A[2] becomes 5.
After 2 operations A[] becomes {0, 1, 5, 3} whose every subarray of size 3 is at least X = 5.
Approach: Implement the idea below to solve the problem
The problem can be solved using Dynamic Programming. Since any element can belong to at most 3 subarrays, we have K cases for any index i:
- Case 1: Increment ith index and move to index (i+1)
- Case 2: Increment (i + 1)th index and move to index (i + 2)
- Case 3: Increment (i + 2)th index and move to index (i + 3)
- ….
- Case K: Increment (i + K – 1)th index and move to index (i + K)
Now, we can explore all the cases to get the answer.
Steps to solve the problem:
- Maintain a recursive function minOperations(idx, X, N, K) to return the answer from index idx to index N-1.
- Iterate from i = 0 to i = K-1,
- Explore the case where we increment A[idx + i] and call minOperations(idx + i + 1, X, N, K).
- Minimize the answer over all choices.
- Return the minimum operations required.
Code to implement the approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperations( int A[], int idx, int X, int N, int K,
vector< long long >& dp)
{
if (idx > N - K)
return 0;
if (dp[idx] != -1)
return dp[idx];
long long ans = 1e18;
for ( int i = 0; i < K; i++) {
long long choose
= max(0, X - A[idx + i])
+ minOperations(A, idx + i + 1, X, N, K, dp);
ans = min(ans, choose);
}
return dp[idx] = ans;
}
int main()
{
int N = 5, X = 4, K = 3;
int A[] = { 2, 3, 0, 0, 2 };
vector< long long > dp(N, -1);
cout << minOperations(A, 0, X, N, K, dp) << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class MinimumOperations {
static int minOperations( int [] A, int idx, int X, int N, int K, int [] dp) {
if (idx > N - K) {
return 0 ;
}
if (dp[idx] != - 1 ) {
return dp[idx];
}
int ans = Integer.MAX_VALUE;
for ( int i = 0 ; i < K; i++) {
int choose = Math.max( 0 , X - A[idx + i]) +
minOperations(A, idx + i + 1 , X, N, K, dp);
ans = Math.min(ans, choose);
}
dp[idx] = ans;
return ans;
}
public static void main(String[] args) {
int N = 5 , X = 4 , K = 3 ;
int [] A = { 2 , 3 , 0 , 0 , 2 };
int [] dp = new int [N];
Arrays.fill(dp, - 1 );
System.out.println(minOperations(A, 0 , X, N, K, dp));
}
}
|
Python3
def min_operations(A, idx, X, N, K, dp):
if idx > N - K:
return 0
if dp[idx] ! = - 1 :
return dp[idx]
ans = float ( 'inf' )
for i in range (K):
choose = max ( 0 , X - A[idx + i]) + \
min_operations(A, idx + i + 1 , X, N, K, dp)
ans = min (ans, choose)
dp[idx] = ans
return ans
if __name__ = = "__main__" :
N, X, K = 5 , 4 , 3
A = [ 2 , 3 , 0 , 0 , 2 ]
dp = [ - 1 ] * N
print (min_operations(A, 0 , X, N, K, dp))
|
C#
using System;
public class GFG {
static int MinOperations( int [] A, int idx, int X, int N,
int K, int [] dp)
{
if (idx > N - K) {
return 0;
}
if (dp[idx] != -1) {
return dp[idx];
}
int ans = int .MaxValue;
for ( int i = 0; i < K; i++) {
int choose = Math.Max(0, X - A[idx + i])
+ MinOperations(A, idx + i + 1, X,
N, K, dp);
ans = Math.Min(ans, choose);
}
dp[idx] = ans;
return ans;
}
static void Main()
{
int N = 5, X = 4, K = 3;
int [] A = { 2, 3, 0, 0, 2 };
int [] dp = new int [N];
for ( int i = 0; i < N; i++) {
dp[i] = -1;
}
Console.WriteLine(MinOperations(A, 0, X, N, K, dp));
}
}
|
Javascript
function minOperations(A, idx, X, N, K, dp) {
if (idx > N - K) {
return 0;
}
if (dp[idx] !== -1) {
return dp[idx];
}
let ans = Infinity;
for (let i = 0; i < K; i++) {
const choose = Math.max(0, X - A[idx + i]) +
minOperations(A, idx + i + 1, X, N, K, dp);
ans = Math.min(ans, choose);
}
dp[idx] = ans;
return ans;
}
const N = 5, X = 4, K = 3;
const A = [2, 3, 0, 0, 2];
const dp = Array(N).fill(-1);
console.log(minOperations(A, 0, X, N, K, dp));
|
Time Complexity: O(N), where N is the size of the input array A[].
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...