Minimum Operations to Make the Array Increasing - Problem

You are given an integer array nums (0-indexed). In one operation, you can choose an element of the array and increment it by 1.

For example, if nums = [1,2,3], you can choose to increment nums[1] to make nums = [1,3,3].

Return the minimum number of operations needed to make nums strictly increasing.

An array nums is strictly increasing if nums[i] < nums[i+1] for all 0 <= i < nums.length - 1. An array of length 1 is trivially strictly increasing.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,1,1]
Output: 3
💡 Note: After 3 operations: [1,2,3]. Operations: increment nums[1] to 2 (1 op), increment nums[2] to 3 (2 ops).
Example 2 — Partially Sorted
$ Input: nums = [1,5,2,4,1]
Output: 14
💡 Note: Transform to [1,5,6,7,8]. Operations: nums[2]: 2→6 (4 ops), nums[3]: 4→7 (3 ops), nums[4]: 1→8 (7 ops). Total: 14.
Example 3 — Already Sorted
$ Input: nums = [8]
Output: 0
💡 Note: Single element array is already strictly increasing, no operations needed.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ 1000

Visualization

Tap to expand
Minimum Operations to Make Array Increasing INPUT nums = [1, 1, 1] 1 i=0 1 i=1 1 i=2 Requirement: Make array strictly increasing NOT Strictly Increasing! 1 NOT < 1 NOT < 1 Need: nums[i] < nums[i+1] for all valid i ALGORITHM STEPS 1 Initialize ops = 0, start at i=1 2 i=1: Compare nums[1] 1 <= 1, need 2 ops += 1, nums[1] = 2 3 i=2: Compare nums[2] 1 <= 2, need 3 ops += 2, nums[2] = 3 4 Return total ops ops = 1 + 2 = 3 Operation Tracking i old new ops 0 1 1 0 1 1 2 +1 2 1 3 +2 FINAL RESULT Final Array (conceptual): 1 i=0 2 +1 op 3 +2 ops < < Strictly Increasing! 1 < 2 < 3 -- OK OUTPUT 3 Total operations: 3 (1 + 2 = 3 increments) to achieve [1,2,3] Key Insight: Greedy approach: For each element, if it's not greater than the previous one, increment it to previous + 1. The minimum operations needed = (prev + 1) - current. Track running total of operations. Time: O(n) | Space: O(1) - Single pass through the array with constant extra space. TutorialsPoint - Minimum Operations to Make the Array Increasing | Greedy Single Pass
Asked in
Google 15 Amazon 12 Facebook 8
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen