Title: Minimum Number of Increments to Make Array Strictly Increasing
In this guide, we will solve a classic greedy array problem: given an integer array, find the minimum number of +1 operations needed so that every element is strictly greater than the one before it. Each operation picks one element and increases it by exactly 1.
You will learn why a single left-to-right pass is enough, how to count the operations without actually simulating each increment, and why the running total should be a long. Let’s explore the idea step by step.
Understanding the Problem
An array is strictly increasing when nums[0] < nums[1] < nums[2] .... Equal neighbours are not allowed. We may only increase values, never decrease them.
For [1, 2, 2] the last two elements are equal, so we raise the last element once to get [1, 2, 3]. The answer is 1.
Approach / Algorithm Steps
Because we can only increase values, every element must be at least one more than its (already fixed) left neighbour. Raising an element by exactly the shortfall is the cheapest option, and it also keeps the next element’s requirement as small as possible.
- Keep a variable
previousholding the final value of the element to the left. Start it asnums[0]. - Keep a counter
operationsstarting at 0. - Walk through the array from index 1 to the end.
- If
nums[i] > previous, the element is already fine. Setprevious = nums[i]. - Otherwise compute
target = previous + 1, addtarget - nums[i]tooperations, and setprevious = target. - After the loop, return
operations.
Java Program
public class MinIncrementsStrictlyIncreasing {
public static long minIncrements(int[] nums) {
if (nums == null) {
throw new IllegalArgumentException("Array must not be null");
}
if (nums.length < 2) {
return 0;
}
long operations = 0;
long previous = nums[0];
for (int i = 1; i < nums.length; i++) {
long current = nums[i];
if (current <= previous) {
long target = previous + 1;
operations += target - current;
current = target;
}
previous = current;
}
return operations;
}
public static void main(String[] args) {
int[] a = {1, 2, 2};
int[] b = {3, 2, 1};
int[] c = {1, 5, 9};
System.out.println(minIncrements(a)); // 1
System.out.println(minIncrements(b)); // 6
System.out.println(minIncrements(c)); // 0
}
}Explanation of the Program
minIncrementsrejects anullarray and returns 0 for arrays with fewer than two elements, since they are trivially strictly increasing.previousandcurrentarelongso thatprevious + 1cannot overflow when the array containsInteger.MAX_VALUE.- When
current <= previous, we compute the smallest legal value,previous + 1, and pay the difference. - We never modify the input array. The adjusted value lives only in
previous, which is all the next iteration needs.
Step-by-Step Trace
Let’s trace the input [5, 3, 3, 8, 7, 9]. The expected answer is 10.
| Iteration | nums[i] | previous (before) | nums[i] > previous? | previous (after) | operations (after) |
|---|---|---|---|---|---|
| 1 | 3 | 5 | no | 6 | 3 |
| 2 | 3 | 6 | no | 7 | 7 |
| 3 | 8 | 7 | yes | 8 | 7 |
| 4 | 7 | 8 | no | 9 | 9 |
| 5 | 9 | 9 | no | 10 | 10 |
At the end of each iteration, previous holds the final value of the element at index i in the strictly increasing array, and operations holds the minimum cost to fix nums[0..i].
Examples
Example 1: One Duplicate
int[] nums = {1, 2, 2};
System.out.println(minIncrements(nums));
// Output: 1 -> [1, 2, 3]Example 2: Strictly Decreasing Array
int[] nums = {3, 2, 1};
System.out.println(minIncrements(nums));
// Output: 6 -> [3, 4, 5]Example 3: Already Strictly Increasing
int[] nums = {1, 5, 9};
System.out.println(minIncrements(nums));
// Output: 0 -> no changes neededCommon Mistakes to Avoid
These are the bugs learners most often hit on this problem.
- Comparing with the original neighbour – on
[1, 1, 1]you compare each element with the unmodified left value and answer 0 or 2 instead of 3. Compare against the adjustedprevious. - Using < instead of <= – equal neighbours such as
[2, 2]are wrongly accepted and you return 0. Treat equality as a violation. - Raising to previous instead of previous + 1 – this produces equal neighbours, so the result is only non-decreasing and the count is too low. Use
previous + 1as the target. - Using int for the total – a long decreasing array can push the sum past 2,147,483,647 and the result goes negative. Accumulate in a
long. - Over-incrementing – raising an element more than the shortfall wastes operations and inflates every later requirement. Raise it by exactly
previous + 1 - nums[i].
Edge Cases to Test
Run through these inputs before calling the solution done.
- Single-element array
- Empty array
- Already strictly increasing array
- All identical elements
- Strictly decreasing array
- Negative numbers
- Integer.MAX_VALUE present
- Null array reference
Additional Considerations
The algorithm makes one pass and uses a few scalar variables, so the time complexity is O(n) and the space complexity is O(1).
The Integer.MAX_VALUE case matters because previous + 1 would overflow an int and wrap to a negative value. Working in long keeps the arithmetic safe, and the returned total is a long for the same reason: a decreasing array of length n can need roughly n squared over two operations.
The greedy choice is correct because any valid result needs result[i] >= result[i-1] + 1 and result[i] >= nums[i]. Taking the smaller of the two possibilities at each step never hurts later elements.
Interview Follow-up Questions
Interviewers often extend the base problem in these directions.
- What if you must return the modified array as well? – write
targetback into a copy of the array during the same pass; the complexity stays O(n). - What if the input is read-only and you may not allocate extra arrays? – the scalar-only solution already satisfies this, since only
previousandoperationsare stored. - What if the values arrive as a stream of unknown length? – the loop only needs the previous adjusted value, so process each element as it arrives and keep a running total.
- What if you may also decrement elements and want the minimum total changes? – subtract the index from each element to reduce it to a non-decreasing problem, then use a max-heap (slope trick) for O(n log n).
Conclusion
Making an array strictly increasing with the fewest +1 operations comes down to one idea: raise each element to exactly one more than its adjusted left neighbour, and only when it falls short. That gives a clean O(n) time, O(1) space solution.
Try adapting the code to return the final array or to require a minimum gap of k between neighbours. You can learn more about similar problems in our Arrays category: Arrays tutorials.



