Minimum Replacements to Sort the Array
HARDDescription
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
Approaches
Checkout 2 different approaches to solve Minimum Replacements to Sort the Array. Click on different approaches to view the approach and algorithm in detail.
Naive Greedy Approach (Right-to-Left)
A less optimal greedy approach is to iterate through the array from right to left. When an element nums[i] is found to be greater than the element to its right (next_val), we must split nums[i]. A simple, but naive, way to split nums[i] is to break it down into pieces that are mostly of size next_val. This strategy, while seemingly logical, makes a poor choice for the new constraint on the next element, leading to a suboptimal total number of operations.
Algorithm
- Initialize
operations = 0andnext_val = nums[n-1]. - Iterate from
i = n-2down to0. - If
nums[i] <= next_val, the array is locally sorted. Updatenext_val = nums[i]and continue. - If
nums[i] > next_val, a split is necessary. - Calculate operations needed for a naive split:
ops_needed = (nums[i] - 1) / next_val. Add this tooperations. - Update
next_valfor the next iteration. The newnext_valisnums[i] % next_val. If the remainder is 0, it'snext_valitself. - Return total
operations.
This approach processes the array from right to left, ensuring that each element is less than or equal to the element that follows it. When nums[i] > next_val, where next_val is the value of the subsequent element (or the first part of it if it was split), nums[i] must be broken down.
The naive splitting strategy is as follows: break nums[i] into q = floor(nums[i] / next_val) pieces of size next_val, and one remainder piece r = nums[i] % next_val. For example, if nums[i] = 19 and next_val = 8, we split 19 into 3, 8, 8. This requires q=2 operations. The new constraint for nums[i-1] becomes the smallest piece of this split, which is r=3 (or next_val if r=0).
This choice is suboptimal because making the new constraint (next_val) as small as nums[i] % next_val can cause a cascade of additional, unnecessary splits for elements to the left.
class Solution {
public long minimumReplacement(int[] nums) {
int n = nums.length;
long operations = 0;
long next_val = nums[n - 1];
for (int i = n - 2; i >= 0; i--) {
if (nums[i] <= next_val) {
next_val = nums[i];
continue;
}
long current_val = nums[i];
// This calculation finds the number of operations based on this naive split.
// It's equivalent to floor(current_val / next_val) if there's a remainder,
// and floor(current_val / next_val) - 1 if there's no remainder.
long num_ops = (current_val - 1) / next_val;
operations += num_ops;
// The new constraint for the left element is the remainder of the division.
long remainder = current_val % next_val;
next_val = (remainder == 0) ? next_val : remainder;
}
return operations;
}
}
Complexity Analysis
Pros and Cons
- The approach is simple to understand.
- It correctly identifies the necessary condition for splitting.
- It has an efficient time and space complexity.
- This greedy strategy is not optimal. It does not guarantee the minimum number of replacements because its choice of the next constraint (
next_val) is too strict, potentially increasing future operations.
Code Solutions
Checking out 3 solutions in different languages for Minimum Replacements to Sort the Array. Click on different languages to view the code.
Video Solution
Watch the video walkthrough for Minimum Replacements to Sort the Array
Similar Questions
5 related questions you might find useful
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.