Skip to main content

Minimum Increment Operations to Make Array Beautiful - Solution & Explanation

MediumArrayDynamic Programming16 min readAsked at: Google
Practice this problem

Problem Statement

You are given a 0-indexed integer array nums having length n, and an integer k.

You can perform the following increment operation any number of times (including zero):

  • Choose an index i in the range [0, n - 1], and increase nums[i] by 1.

An array is considered beautiful if, for any subarray with a size of 3 or more, its maximum element is greater than or equal to k.

Return an integer denoting the minimum number of increment operations needed to make nums beautiful.

A subarray is a contiguous non-empty sequence of elements within an array.

 

Example 1:

Input: nums = [2,3,0,0,2], k = 4
Output: 3
Explanation: We can perform the following increment operations to make nums beautiful:
Choose index i = 1 and increase nums[1] by 1 -> [2,4,0,0,2].
Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,3].
Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,4].
The subarrays with a size of 3 or more are: [2,4,0], [4,0,0], [0,0,4], [2,4,0,0], [4,0,0,4], [2,4,0,0,4].
In all the subarrays, the maximum element is equal to k = 4, so nums is now beautiful.
It can be shown that nums cannot be made beautiful with fewer than 3 increment operations.
Hence, the answer is 3.

Example 2:

Input: nums = [0,1,3,3], k = 5
Output: 2
Explanation: We can perform the following increment operations to make nums beautiful:
Choose index i = 2 and increase nums[2] by 1 -> [0,1,4,3].
Choose index i = 2 and increase nums[2] by 1 -> [0,1,5,3].
The subarrays with a size of 3 or more are: [0,1,5], [1,5,3], [0,1,5,3].
In all the subarrays, the maximum element is equal to k = 5, so nums is now beautiful.
It can be shown that nums cannot be made beautiful with fewer than 2 increment operations.
Hence, the answer is 2.

Example 3:

Input: nums = [1,1,2], k = 1
Output: 0
Explanation: The only subarray with a size of 3 or more in this example is [1,1,2].
The maximum element, 2, is already greater than k = 1, so we don't need any increment operation.
Hence, the answer is 0.

 

Constraints:

  • 3 <= n == nums.length <= 105
  • 0 <= nums[i] <= 109
  • 0 <= k <= 109

Approach Overview

Problem Overview: You are given an integer array nums and a target value k. You can increment any element by 1 per operation. The array is considered beautiful if every subarray of length 3 contains at least one element greater than or equal to k. The goal is to compute the minimum number of increment operations required.

Approach 1: Greedy Increment Strategy (O(n) time, O(1) space)

Start by computing the cost to raise each element to k: cost[i] = max(0, k - nums[i]). For every window of length three, at least one index must be upgraded so its value reaches k. The greedy idea is to treat each upgraded index as covering the windows that include it. Track the last few positions that could satisfy the constraint and always choose the option with the minimum accumulated cost. This works because upgrading an index automatically satisfies up to three overlapping windows, so picking the cheapest candidate among nearby indices minimizes total operations.

Approach 2: Sliding Window with Running Minimum (Dynamic Programming) (O(n) time, O(1) space)

This approach models the problem as a dynamic programming transition. Let dp[i] represent the minimum operations needed if index i is the element that satisfies the window ending at i. The cost to use this index is cost[i] = max(0, k - nums[i]). Since any window of size three must contain at least one upgraded element, the previous valid index must be within the last three positions. The recurrence becomes dp[i] = cost[i] + min(dp[i-1], dp[i-2], dp[i-3]). Maintain the running minimum over the previous three states instead of storing the entire array. This effectively behaves like a small sliding window over DP states while iterating through the array. The final answer is the minimum among the last three states because the last window may end at any of them.

Recommended for interviews: The dynamic programming formulation with a sliding window minimum is the expected solution. It clearly shows how overlapping windows translate into state transitions and achieves optimal O(n) time with constant space. Starting from the greedy intuition helps explain why only the last three indices matter, but the DP formulation demonstrates stronger problem‑solving structure.

Approach 1: Sliding Window with Running Maximum

This approach involves maintaining a running maximum for each subarray of size at least 3. If the maximum is less than k, increment the necessary elements to achieve the desired maximum. This can be done efficiently with a sliding window technique.

This solution uses a sliding window technique, iterating through the array and maintaining a running maximum for each subarray of size at least 3. It only increments elements when required, minimizing the total increments.

Code

Python

Java

C++

C#

JavaScript

Complexity

Time complexity: O(n) where n is the length of the array. Space complexity: O(1) as we only use constant extra space.

Try this approach in the editor →

Approach 2: Greedy Increment Strategy

The greedy strategy involves iterating through the array and incrementing only when the current maximum found within the sliding window is less than k. The key is to immediately adjust the necessary element to meet the subarray criterion.

This greedy method directly computes and increments deficiencies from the expected maximum value iteratively, ensuring all relevant subarrays contain a maximum value >= k.

Code

Python

Java

C++

C#

JavaScript

Complexity

Time complexity: O(n). Space complexity: O(1).

Try this approach in the editor →

Approach 3: Dynamic Programming

We define f, g, and h as the minimum number of increment operations needed to get the maximum value from the last three items in the first i items, initially f = 0, g = 0, h = 0.

Next, we traverse the array nums. For each x, we need to update the values of f, g, and h to meet the requirements of the problem, that is:

$ \begin{aligned} f' &= g \ g' &= h \ h' &= min(f, g, h) + max(k - x, 0) \end{aligned}

Finally, we only need to return the minimum value among f, g, and h.

The time complexity is O(n), where n is the length of the array. The space complexity is O(1)$.

Code

Python

Java

C++

Go

TypeScript

C#

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Sliding Window with Running Maximum

Time complexity: O(n) where n is the length of the array. Space complexity: O(1) as we only use constant extra space.

Greedy Increment Strategy

Time complexity: O(n). Space complexity: O(1).

Dynamic Programming—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Greedy Increment StrategyO(n)O(1)When you reason about covering each length‑3 window with the cheapest upgrade candidate.
Sliding Window with Running Minimum (DP)O(n)O(1)Best general solution. Converts overlapping window constraints into DP with the minimum of the last three states.

Video Solution

2919. Minimum Increment Operations to Make Array Beautiful || Greedy ❌ || Dynamic Programming ✅ • Ayush Rao • 1,804 views views

Watch 6 more video solutions →

Frequently Asked Questions

Is Minimum Increment Operations to Make Array Beautiful easy or hard?
The problem is rated Medium because the brute force idea is simple but recognizing the dynamic programming transition requires insight. Once you see that only the previous three indices affect the current decision, the solution becomes a straightforward O(n) DP.
Minimum Increment Operations to Make Array Beautiful Python/Java solution
Most implementations compute the upgrade cost for each element and maintain three DP states representing the minimum cost for the previous indices. The recurrence dp[i] = cost[i] + min(dp[i-1], dp[i-2], dp[i-3]) can be implemented in Python, Java, C++, C#, or JavaScript using simple variables instead of a full array.
How to solve Minimum Increment Operations to Make Array Beautiful in O(n)?
First compute cost[i] = max(0, k - nums[i]). Use dynamic programming where dp[i] = cost[i] + min(dp[i-1], dp[i-2], dp[i-3]). This ensures each window of size three contains at least one index upgraded to k. Track only the last three DP values to achieve O(n) time and constant space.
What is the best approach for Minimum Increment Operations to Make Array Beautiful?
The optimal solution uses dynamic programming with a sliding window minimum. For each index i, compute the cost to raise nums[i] to k and combine it with the minimum cost among the previous three DP states. This ensures every subarray of length three contains at least one upgraded element. The algorithm runs in O(n) time with O(1) extra space.
Is Minimum Increment Operations to Make Array Beautiful asked at Google/Amazon/Meta?
Problems with overlapping window constraints and DP transitions like this commonly appear in interviews at companies such as Amazon, Google, and Meta. The pattern of choosing minimum cost among the previous few states is a standard dynamic programming interview technique.
What data structure is used in Minimum Increment Operations to Make Array Beautiful?
The solution primarily uses arrays and dynamic programming state tracking. A small sliding window over the last three DP values acts as the main structure, allowing the algorithm to compute the minimum cost efficiently while scanning the array.
What is the time complexity of Minimum Increment Operations to Make Array Beautiful?
The optimal algorithm runs in O(n) time because the array is processed once while maintaining the minimum of the last three DP states. Space complexity can be reduced to O(1) since only three previous values are required instead of a full DP array.

Ready to solve this problem?

Practice Minimum Increment Operations to Make Array Beautiful with our built-in code editor and test cases.

Practice on FleetCode