Skip to main content

Minimum Operations to Make Array Non Decreasing - Solution & Explanation

MediumArrayGreedy6 min readAsked at: Amazon
Practice this problem

Problem Statement

You are given an integer array nums of length n.

In one operation, you may choose any subarray nums[l..r] and increase each element in that subarray by x, where x is any positive integer.

Return the minimum possible sum of the values of x across all operations required to make the array non-decreasing.

An array is non-decreasing if nums[i] <= nums[i + 1] for all 0 <= i < n - 1.

 

Example 1:

Input: nums = [3,3,2,1]

Output: 2

Explanation:

One optimal set of operations:

  • Choose subarray [2..3] and add x = 1 resulting in [3, 3, 3, 2]
  • Choose subarray [3..3] and add x = 1 resulting in [3, 3, 3, 3]

The array becomes non-decreasing, and the total sum of chosen x values is 1 + 1 = 2.

Example 2:

Input: nums = [5,1,2,3]

Output: 4

Explanation:

One optimal set of operations:

  • Choose subarray [1..3] and add x = 4 resulting in [5, 5, 6, 7]

The array becomes non-decreasing, and the total sum of chosen x values is 4.

 

Constraints:

  • 1 <= n == nums.length <= 105
  • 1 <= nums[i] <= 109

Approach Overview

Problem Overview: You are given an integer array and need the minimum number of operations required to transform it into a non‑decreasing sequence. A non‑decreasing array means nums[i] >= nums[i-1] for every index. The goal is to determine how many adjustments are required so every element respects this ordering.

Approach 1: Brute Force Increment Simulation (O(n * d) time, O(1) space)

The most direct idea is to scan the array from left to right and check whether the current element violates the non‑decreasing condition. If nums[i] < nums[i-1], repeatedly increment nums[i] until it becomes equal to nums[i-1]. Each increment counts as one operation. This simulation works but becomes inefficient when the difference between elements is large because every unit increment is processed individually. The logic is simple and useful for understanding the mechanics of the problem, but its runtime can degrade significantly.

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

A better observation removes the need for repeated increments. If nums[i] < nums[i-1], the minimum adjustment required is exactly nums[i-1] - nums[i]. Instead of simulating each step, add this difference directly to the operation count and treat the current element as if it has been raised to match nums[i-1]. This greedy idea works because increasing an element to the smallest valid value keeps future adjustments minimal. You process the array once, maintain the previous valid value, and accumulate required operations.

Approach 3: Prefix Maximum Tracking (O(n) time, O(1) space)

This variation frames the same greedy logic using a running prefix maximum. Maintain prev_max, the maximum value seen so far while scanning left to right. If the current number is smaller than prev_max, it must be raised to that value, contributing prev_max - nums[i] operations. Otherwise, update prev_max to the current value. Conceptually, you enforce the invariant that every element equals at least the maximum value to its left. This approach is easy to implement and commonly used in arrays and greedy style interview problems.

Recommended for interviews: The greedy single‑pass approach using a prefix maximum is the expected solution. Interviewers typically accept the brute‑force explanation as a starting point, but the optimized method shows you recognize the exact adjustment required without simulation. The final algorithm runs in linear time, uses constant space, and relies on a simple invariant maintained during iteration. Problems that enforce ordering constraints like this often appear in array processing and greedy strategy discussions.

Solution

We can traverse the array from left to right and calculate the difference between each pair of adjacent elements. If the current element is smaller than the previous one, we need to increase the current element so that it is at least equal to the previous element. The amount to increase is the difference between the previous element and the current element.

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

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Increment SimulationO(n * d)O(1)Good for understanding the mechanics when differences between elements are small
Greedy Difference AccumulationO(n)O(1)Best general solution; computes required increments directly
Prefix Maximum TrackingO(n)O(1)Preferred interview implementation using a running maximum invariant

Video Solution

Minimum Operations to Make Array Non-Decreasing | LeetCode 3914 | Subarray Increment StrategyTechdose806 views views

Watch 8 more video solutions →

Frequently Asked Questions

Is Minimum Operations to Make Array Non Decreasing easy or hard?
The problem is typically categorized as Medium. The brute force idea is simple, but recognizing that you can directly add the required difference instead of simulating each increment requires a greedy insight. Once that observation is made, the implementation becomes a clean O(n) scan.
Minimum Operations to Make Array Non Decreasing Python/Java solution
The implementation is straightforward in Python, Java, or C++. Maintain a variable storing the maximum value seen so far. When a smaller element appears, add the difference to the answer and treat the element as updated to the maximum. The code uses a single loop and constant extra memory.
How to solve Minimum Operations to Make Array Non Decreasing in O(n)?
Iterate through the array while maintaining a variable like `prev_max`. If the current value is less than `prev_max`, add `prev_max - nums[i]` to the operation count. Otherwise update `prev_max` to the current value. This greedy strategy ensures each element becomes at least as large as the previous element with minimal adjustments.
What is the best approach for Minimum Operations to Make Array Non Decreasing?
The best approach is a greedy single-pass algorithm that tracks the maximum value seen so far. When the current element is smaller than this value, increase it conceptually to match and add the difference to the operation count. This guarantees the minimal adjustment needed while maintaining the non-decreasing property. The algorithm runs in O(n) time and O(1) space.
Is Minimum Operations to Make Array Non Decreasing asked at Google/Amazon/Meta?
Variants of this problem appear in interviews at large tech companies because it tests greedy reasoning and array traversal patterns. Companies like Amazon, Google, and Meta frequently ask questions involving maintaining order constraints, prefix maxima, or minimal adjustments to arrays.
What data structure is used in Minimum Operations to Make Array Non Decreasing?
The solution primarily relies on simple array traversal and a few scalar variables. No complex data structures are required. The algorithm keeps track of a running prefix maximum and a counter for total operations while scanning the array.
What is the time complexity of Minimum Operations to Make Array Non Decreasing?
The optimal solution runs in O(n) time because the array is scanned once from left to right. Each element requires only constant work: compare with the previous maximum and accumulate the difference if needed. Space complexity is O(1) since only a few variables such as the running maximum and operation counter are maintained.

Ready to solve this problem?

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

Practice on FleetCode