Skip to main content

Minimum Number of Increments on Subarrays to Form a Target Array - Solution & Explanation

HardArrayDynamic ProgrammingStackGreedy19 min readAsked at: Amazon, Microsoft, Meta +4
Practice this problem

Problem Statement

You are given an integer array target. You have an integer array initial of the same size as target with all elements initially zeros.

In one operation you can choose any subarray from initial and increment each value by one.

Return the minimum number of operations to form a target array from initial.

The test cases are generated so that the answer fits in a 32-bit integer.

 

Example 1:

Input: target = [1,2,3,2,1]
Output: 3
Explanation: We need at least 3 operations to form the target array from the initial array.
[0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
[1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
[1,2,2,2,1] increment 1 at index 2.
[1,2,3,2,1] target array is formed.

Example 2:

Input: target = [3,1,1,2]
Output: 4
Explanation: [0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2]

Example 3:

Input: target = [3,1,5,4,2]
Output: 7
Explanation: [0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1] -> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2].

 

Constraints:

  • 1 <= target.length <= 105
  • 1 <= target[i] <= 105

Approach Overview

Problem Overview: You start with an array of zeros and can increment any subarray by 1 in a single operation. The goal is to construct the given target array using the minimum number of such operations.

Approach 1: Difference Array Method (O(n) time, O(1) space)

The key observation is that increments only matter when the array value increases compared to the previous index. If target[i] is greater than target[i-1], you must start additional increment operations equal to the difference. When the value decreases or stays the same, previously started operations already cover that segment. Iterate through the array once, compute max(0, target[i] - target[i-1]), and accumulate the result. Conceptually, this acts like a difference array where positive increases represent new subarray increments that must start at that position. The approach is extremely efficient and only tracks the previous value, making the space complexity constant.

This technique frequently appears in array transformation problems where operations propagate across ranges. Instead of simulating each increment operation, you compute how many operations must start at each index.

Approach 2: Greedy Incrementation (O(n) time, O(1) space)

The greedy interpretation views the target array as layers of increments stacked vertically. Every time the height increases compared to the previous position, a new layer must start. If the height drops, some layers simply end earlier. The minimum operations equal the sum of all positive height increases while scanning from left to right. Initialize the answer with target[0] since you must perform that many operations starting at index 0, then iterate and add target[i] - target[i-1] whenever the difference is positive.

This greedy insight avoids complex simulation or dynamic programming. It also connects to patterns seen in greedy and monotonic stack problems where only upward transitions create new work. Even though the problem is labeled Hard, the optimal solution is a simple linear pass once the correct observation is made.

Recommended for interviews: The greedy/difference-array reasoning is what interviewers expect. Explaining why only positive increases matter demonstrates strong problem decomposition. Showing a naive simulation first can help communicate understanding, but the optimal O(n) scan proves you can identify the structural pattern behind range increment operations.

Approach 1: Difference Array Method

To solve the problem optimally, we can use the concept of differences between consecutive elements in the target array. The number of operations required to achieve the target at each index is dictated by the difference between the current element and the previous element in the target array. By summing all the positive differences, we get the total number of operations needed.

Here's why this works: starting from the initial array (which is all zeros), each increment can be seen as targeting a 'range' of the array. The increments impose certain requirements for transitions between consecutive numbers. Therefore, the sum of necessary transitions gives us the minimum operations required.

The solution starts by identifying the minimum number of operations needed as the first element of the target since we have to create that amount starting from zero. As we iterate over the target array, if a position 'i' has a value larger than its previous position, it's necessary to increment the difference to account for it. The sum of those required transitions is our answer.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the target array, since we iterate over the array once.
Space Complexity: O(1), since we only use a constant amount of extra space.

Try this approach in the editor →

Approach 2: Greedy Incrementation

Another approach to solve this problem is using a greedy strategy that incrementally builds the target array value-by-value, choosing increments as needed to transition between values in the target. This method efficiently considers the necessary increments at each step by analyzing both the current and previous elements.

This is done by incrementally forming the target using the fewest number of operations required when seen as a series of number levels that need reaching.

In this C solution, the function iterates through the target array while maintaining a 'previous' value that starts at zero. For each target element, if it's greater than 'previous', the difference is added to operations, effectively counting increments needed.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the total number of elements in the target array.
Space Complexity: O(1), since we're using constant extra space.

Try this approach in the editor →

Approach 3: Dynamic Programming

We define f[i] as the minimum number of operations required to obtain target[0,..i], initially setting f[0] = target[0].

For target[i], if target[i] leq target[i-1], then f[i] = f[i-1]; otherwise, f[i] = f[i-1] + target[i] - target[i-1].

The final answer is f[n-1].

We notice that f[i] only depends on f[i-1], so we can maintain the operation count using just one variable.

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

Similar problems:

Code

Python

Java

C++

Go

TypeScript

Rust

C#

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Difference Array Method

Time Complexity: O(n), where n is the length of the target array, since we iterate over the array once.
Space Complexity: O(1), since we only use a constant amount of extra space.

Greedy Incrementation

Time Complexity: O(n), where n is the total number of elements in the target array.
Space Complexity: O(1), since we're using constant extra space.

Dynamic Programming—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Difference Array MethodO(n)O(1)Best general solution. Uses positive differences to count new subarray increments.
Greedy IncrementationO(n)O(1)Interview-friendly explanation using height increases between adjacent elements.

Video Solution

Minimum Operations to Make Array Equal to Target | Minimum Number of Increments|Leetcode 3229 & 1526 • codestorywithMIK • 13,853 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Minimum Number of Increments on Subarrays to Form a Target Array easy or hard?
The problem is labeled Hard on LeetCode because the correct insight is not immediately obvious. Once you recognize that only upward differences require new operations, the implementation becomes a simple O(n) greedy scan.
Minimum Number of Increments on Subarrays to Form a Target Array Python/Java solution
In Python, Java, C++, or JavaScript, the implementation is nearly identical. Initialize the result with target[0], iterate through the array, and add the positive difference between consecutive elements. The algorithm runs in O(n) time and uses O(1) extra memory across all languages.
How to solve Minimum Number of Increments on Subarrays to Form a Target Array in O(n)?
Start with result = target[0] because that many operations must begin at index 0. Then iterate from index 1 to n-1 and add max(0, target[i] - target[i-1]) to the result. Each positive increase represents new subarray increments that must start at that position. This single pass computes the minimum number of operations.
What is the best approach for Minimum Number of Increments on Subarrays to Form a Target Array?
The optimal solution uses a greedy difference observation. Scan the array and count only the positive increases between adjacent elements. Whenever target[i] > target[i-1], you must start target[i] - target[i-1] new increment operations. This produces an O(n) time and O(1) space solution.
Is Minimum Number of Increments on Subarrays to Form a Target Array asked at Google/Amazon/Meta?
This style of greedy array transformation problem appears in interviews at companies like Google, Amazon, and Meta. It tests whether candidates can recognize patterns in range updates and reduce them to differences between adjacent elements instead of simulating operations.
What data structure is used in Minimum Number of Increments on Subarrays to Form a Target Array?
The optimal solution mainly relies on arrays and a greedy difference calculation. Conceptually it relates to difference arrays and ideas used in monotonic stack problems, but the final implementation only needs a single pass through the array with constant extra space.
What is the time complexity of Minimum Number of Increments on Subarrays to Form a Target Array?
The optimal greedy or difference-array approach runs in O(n) time because the array is scanned once. Only simple arithmetic comparisons are performed per element. Space complexity is O(1) since the algorithm stores only the previous value and the running total.

Ready to solve this problem?

Practice Minimum Number of Increments on Subarrays to Form a Target Array with our built-in code editor and test cases.

Practice on FleetCode