Open In App

Minimum number of operations to make maximum element of every subarray of size K at least X

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return minimum number of operations to
// make maximum element of every subarray of size K
// at least X in from index = idx to N-1
int minOperations(int A[], int idx, int X, int N, int K,
                  vector<long long>& dp)
{
    // if a subarray of size K cannot be formed at index idx
    if (idx > N - K)
        return 0;
    // If the answer is already calculated, return it
    if (dp[idx] != -1)
        return dp[idx];
    long long ans = 1e18;
    // Explore all the choices
    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;
}
 
// Driver Code
int main()
{
 
    // Input
    int N = 5, X = 4, K = 3;
    int A[] = { 2, 3, 0, 0, 2 };
    vector<long long> dp(N, -1);
 
    // Function Call
    cout << minOperations(A, 0, X, N, K, dp) << endl;
 
    return 0;
}


Java




import java.util.Arrays;
 
public class MinimumOperations {
 
    // Function to return minimum number of operations to
    // make the maximum element of every subarray of size K
    // at least X from index idx to N-1
    static int minOperations(int[] A, int idx, int X, int N, int K, int[] dp) {
        // If a subarray of size K cannot be formed at index idx
        if (idx > N - K) {
            return 0;
        }
        // If the answer is already calculated, return it
        if (dp[idx] != -1) {
            return dp[idx];
        }
        int ans = Integer.MAX_VALUE;
        // Explore all the choices
        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;
    }
 
    // Driver Code
    public static void main(String[] args) {
        // Input
        int N = 5, X = 4, K = 3;
        int[] A = {2, 3, 0, 0, 2};
        int[] dp = new int[N];
        Arrays.fill(dp, -1);
 
        // Function Call
        System.out.println(minOperations(A, 0, X, N, K, dp));
    }
}


Python3




# Function to return minimum number of operations to
# make the maximum element of every subarray of size K
# at least X from index idx to N-1
 
 
def min_operations(A, idx, X, N, K, dp):
    # if a subarray of size K cannot be formed at index idx
    if idx > N - K:
        return 0
    # If the answer is already calculated, return it
    if dp[idx] != -1:
        return dp[idx]
    ans = float('inf')
    # Explore all the choices
    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
 
 
# Driver Code
if __name__ == "__main__":
    # Input
    N, X, K = 5, 4, 3
    A = [2, 3, 0, 0, 2]
    dp = [-1] * N
 
    # Function Call
    print(min_operations(A, 0, X, N, K, dp))


C#




// C# program for the above approach
using System;
 
public class GFG {
    // Function to return minimum number of operations to
    // make the maximum element of every subarray of size K
    // at least X from index idx to N-1
    static int MinOperations(int[] A, int idx, int X, int N,
                             int K, int[] dp)
    {
        // If a subarray of size K cannot be formed at index
        // idx
        if (idx > N - K) {
            return 0;
        }
 
        // If the answer is already calculated, return it
        if (dp[idx] != -1) {
            return dp[idx];
        }
 
        int ans = int.MaxValue;
 
        // Explore all the choices
        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;
    }
 
    // Driver Code
    static void Main()
    {
        // Input
        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;
        }
 
        // Function Call
        Console.WriteLine(MinOperations(A, 0, X, N, K, dp));
    }
}
 
// This code is contributed by Susobhan Akhuli


Javascript




// Function to return minimum number of operations to
// make the maximum element of every subarray of size K
// at least X from index idx to N-1
function minOperations(A, idx, X, N, K, dp) {
    // if a subarray of size K cannot be formed at index idx
    if (idx > N - K) {
        return 0;
    }
    // If the answer is already calculated, return it
    if (dp[idx] !== -1) {
        return dp[idx];
    }
    let ans = Infinity;
    // Explore all the choices
    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;
}
 
// Driver Code
// Input
const N = 5, X = 4, K = 3;
const A = [2, 3, 0, 0, 2];
const dp = Array(N).fill(-1);
 
// Function Call
console.log(minOperations(A, 0, X, N, K, dp));


Output

3






Time Complexity: O(N), where N is the size of the input array A[].
Auxiliary Space: O(N)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads