Transform Array to Non-Decreasing with Strategic Pair Merging

You're given an array nums and need to make it non-decreasing (sorted) using a special operation. In each operation, you must:

1. Find all adjacent pairs with the minimum sum
2. If multiple such pairs exist, choose the leftmost one
3. Replace that pair with their sum

Your goal is to find the minimum number of operations needed to make the array non-decreasing.

Example: [3, 2, 1, 4]
• Operation 1: Pairs are (3,2)=5, (2,1)=3, (1,4)=5. Min is 3, so merge (2,1) → [3, 3, 4]
• Result: 1 operation (array is now sorted)

The challenge lies in choosing the optimal sequence of merges to minimize total operations.

Input & Output

example_1.py — Basic Case
$ Input: nums = [3, 2, 1, 4]
Output: 1
💡 Note: Initial array: [3, 2, 1, 4]. Adjacent pairs and their sums: (3,2)→5, (2,1)→3, (1,4)→5. The minimum sum is 3 from pair (2,1), so we merge them: [3, 3, 4]. Now the array is non-decreasing, so we needed 1 operation.
example_2.py — Multiple Operations
$ Input: nums = [4, 3, 2, 1]
Output: 3
💡 Note: Step 1: [4,3,2,1] → pairs (4,3)→7, (3,2)→5, (2,1)→3. Min is 3, merge (2,1) → [4,3,3]. Step 2: [4,3,3] → pairs (4,3)→7, (3,3)→6. Min is 6, merge (3,3) → [4,6]. Step 3: [4,6] → pair (4,6)→10. Min is 10, merge (4,6) → [10]. Total: 3 operations.
example_3.py — Already Sorted
$ Input: nums = [1, 2, 3, 4]
Output: 0
💡 Note: The array [1, 2, 3, 4] is already non-decreasing (1 ≤ 2 ≤ 3 ≤ 4), so no operations are needed.

Visualization

Tap to expand
Minimum Pair Removal SimulationStep 1: Initial Array3214Step 2: Find Adjacent Pairs(3,2) = 5(2,1) = 3 ✓(1,4) = 5Step 3: Merge Minimum Pair (2,1) → 3334← Merged from (2,1)Step 4: Check Result✓ Array [3,3,4] is non-decreasing✓ Total operations needed: 1Algorithm Steps1. Calculate all adjacent pair sums2. Find minimum sum (choose leftmost if tie)3. Merge the chosen pair4. Repeat until array is sorted5. Return operation count
Understanding the Visualization
1
Find All Adjacent Pairs
Calculate sum for each adjacent pair: (3,2)=5, (2,1)=3, (1,4)=5
2
Choose Minimum Sum
Minimum sum is 3 from pair (2,1). Since it's unique, select this pair
3
Merge the Pair
Replace (2,1) with their sum 3, resulting in array [3,3,4]
4
Check if Sorted
Array [3,3,4] is non-decreasing (3≤3≤4), so we're done in 1 operation
Key Takeaway
🎯 Key Insight: The optimal approach uses a priority queue to efficiently track minimum pairs and a doubly-linked list for fast merging, avoiding the need to rescan unchanged portions of the array.

Time & Space Complexity

Time Complexity
⏱️
O(n² log n)

In worst case O(n) operations, each involving O(log n) heap operations and O(n) pairs to track

n
2n
Quadratic Growth
Space Complexity
O(n)

Space for priority queue, doubly-linked list, and tracking structures

n
2n
Linearithmic Space

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ 106
  • You must always choose the leftmost minimum pair when there are ties
  • Array elements can become very large after multiple merge operations
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
26.1K Views
Medium Frequency
~35 min Avg. Time
847 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