Minimum Pair Removal to Sort Array I - Problem
Given an array nums, you can perform the following operation any number of times:
Select the adjacent pair with the minimum sum in nums. If multiple such pairs exist, choose the leftmost one.
Replace the pair with their sum.
Return the minimum number of operations needed to make the array non-decreasing.
An array is said to be non-decreasing if each element is greater than or equal to its previous element (if it exists).
Input & Output
Example 1 — Basic Unsorted Array
$
Input:
nums = [3,4,2,1]
›
Output:
3
💡 Note:
First merge (2,1)→3 giving [3,4,3], then merge (3,4)→7 giving [7,3], finally merge (7,3)→10 giving [10] (sorted). Total: 3 operations.
Example 2 — Already Sorted
$
Input:
nums = [1,2,3,4]
›
Output:
0
💡 Note:
Array is already non-decreasing (1≤2≤3≤4), so no operations needed.
Example 3 — Single Element
$
Input:
nums = [5]
›
Output:
0
💡 Note:
Single element array is trivially non-decreasing, no operations needed.
Constraints
- 1 ≤ nums.length ≤ 1000
- 1 ≤ nums[i] ≤ 1000
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code