Minimum Replacements to Sort the Array - Solution & Explanation
Problem Statement
You are given a 0-indexed integer array nums. In one operation you can replace any element of the array with any two elements that sum to it.
- For example, consider
nums = [5,6,7]. In one operation, we can replacenums[1]with2and4and convertnumsto[5,2,4,7].
Return the minimum number of operations to make an array that is sorted in non-decreasing order.
Example 1:
Input: nums = [3,9,3] Output: 2 Explanation: Here are the steps to sort the array in non-decreasing order: - From [3,9,3], replace the 9 with 3 and 6 so the array becomes [3,3,6,3] - From [3,3,6,3], replace the 6 with 3 and 3 so the array becomes [3,3,3,3,3] There are 2 steps to sort the array in non-decreasing order. Therefore, we return 2.
Example 2:
Input: nums = [1,2,3,4,5] Output: 0 Explanation: The array is already in non-decreasing order. Therefore, we return 0.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 109
Approach Overview
Problem Overview: You are given an integer array and may replace any element with two or more positive integers that sum to it. The goal is to perform the minimum number of replacements so the final array becomes non‑decreasing. Each replacement increases the array length, so the challenge is choosing how many parts each element should be split into while keeping the order valid.
Approach 1: Reverse Approach with Priority Queue (O(n log n) time, O(n) space)
This method processes the array from right to left while maintaining the current allowed maximum value using a priority queue. If the current element is larger than the next allowed value, it must be split into smaller parts. The idea is to repeatedly divide the number so that every generated piece is less than or equal to the next value in the sorted order. Each split contributes to the replacement count, and the resulting pieces are pushed into the heap to maintain ordering. The heap ensures the smallest valid constraint propagates backward while iterating. This approach is conceptually straightforward but introduces O(log n) heap operations for each adjustment.
Approach 2: Greedy Strategy with Two-Pointer Technique (O(n) time, O(1) space)
The optimal strategy scans the array from right to left and keeps track of the maximum allowed value for the current position. Start with the last element as the limit. For each previous element nums[i], check if it already satisfies nums[i] ≤ limit. If it does, simply update the limit to nums[i]. Otherwise, split the value into the minimum number of parts so each part is ≤ limit. The number of required pieces is k = ceil(nums[i] / limit). That means k - 1 replacements are needed. After splitting, the new limit becomes floor(nums[i] / k), which is the largest value each piece can take while keeping the array non‑decreasing.
This greedy insight works because the right side of the array is already fixed. You only need to ensure each element on the left does not violate the current bound. By minimizing the number of pieces at every step, you guarantee the global minimum number of operations. The algorithm uses simple arithmetic and a single backward pass, making it extremely efficient.
Recommended for interviews: Interviewers expect the greedy reverse traversal. It demonstrates strong reasoning about constraints and optimal partitioning. A heap‑based approach shows the intuition behind maintaining order, but the O(n) greedy solution with arithmetic splitting is the cleanest and most efficient. Understanding how the bound propagates backward is key when working with greedy strategies on array problems that involve value partitioning and math reasoning.
Approach 1: Reverse Approach with Priority Queue
This approach works by iterating from the end of the array to the beginning. The idea is to ensure each element is not bigger than the rest of the previously sorted part of the array. This can be optimized using a priority queue to store the required values efficiently, making it easier to decide where to split large numbers.
Whenever we find a number that is greater than the required maximum, we split it and keep track of splits using the priority queue (min-heap in this case).
The solution uses a priority queue to manage elements efficiently. By iterating the array from end to start, we can ensure that each number is processed in a priority-wise manner, allowing us to minimize operations by only splitting when necessary.
Code
Python
JavaScript
Complexity
Time Complexity: O(N log N), primarily due to the heap operations.
Space Complexity: O(N) due to the additional space used by the priority queue.
Approach 2: Greedy Strategy with Two-Pointer Technique
This approach relies on a greedy approach combined with a two-pointer technique. The algorithm will scan the array while managing smaller replacements for larger numbers. The idea is to minimize the change needed by checking previous and current indices simultaneously to determine necessary operations.
The C++ solution uses a vector to maintain the smaller elements found during the traversal from end to start of the array, making controlled adjustments to ensure non-decreasing order.
We maintain order using sorting and check conditions dynamically while iterating.
Complexity
Time Complexity: O(N^2) with sorting for maintaining order.
Space Complexity: O(N) due to the auxiliary vector space used.
Approach 3: Greedy Approach
We observe that to make the array nums non-decreasing or monotonically increasing, the elements towards the end of the array should be as large as possible. Therefore, it is unnecessary to replace the last element nums[n-1] of the array nums with multiple smaller numbers.
In other words, we can traverse the array nums from the end to the beginning, maintaining the current maximum value mx, initially mx = nums[n-1].
- If the current element
nums[i] leq mx, there is no need to replacenums[i]. We simply updatemx = nums[i]. - Otherwise, we need to replace
nums[i]with multiple numbers that sum tonums[i]. The maximum of these numbers ismx, and the total number of replacements isk=\left \lceil \frac{nums[i]}{mx} \right \rceil. Therefore, we need to performk-1operations, which are added to the answer. Among theseknumbers, the smallest number is\left \lfloor \frac{nums[i]}{k} \right \rfloor. Therefore, we updatemx = \left \lfloor \frac{nums[i]}{k} \right \rfloor.
After the traversal, we return the total number of operations.
The time complexity is O(n), where n is the length of the array nums. The space complexity is O(1).
Complexity Comparison
| Approach | Complexity |
|---|---|
| Reverse Approach with Priority Queue | Time Complexity: O(N log N), primarily due to the heap operations. |
| Greedy Strategy with Two-Pointer Technique | Time Complexity: O(N^2) with sorting for maintaining order. |
| Greedy Approach | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Reverse with Priority Queue | O(n log n) | O(n) | When simulating the splitting process explicitly and maintaining ordering via a heap |
| Greedy Reverse Traversal (Two-Pointer Style) | O(n) | O(1) | Optimal interview solution; single backward pass with arithmetic partitioning |
| Naive Simulation | O(n²) | O(n) | Educational baseline to understand why greedy partitioning is necessary |
Video Solution
Minimum Replacements to Sort the Array | Cleanest Code | Full Dry Run | GOOGLE | Leetcode - 2366 • codestorywithMIK • 11,140 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Minimum Replacements to Sort the Array easy or hard?
Minimum Replacements to Sort the Array Python/Java solution
How to solve Minimum Replacements to Sort the Array in O(n)?
What is the best approach for Minimum Replacements to Sort the Array?
Is Minimum Replacements to Sort the Array asked at Google/Amazon/Meta?
What data structure is used in Minimum Replacements to Sort the Array?
What is the time complexity of Minimum Replacements to Sort the Array?
Ready to solve this problem?
Practice Minimum Replacements to Sort the Array with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor