Make Lexicographically Smallest Array by Swapping Elements - Problem
Make Lexicographically Smallest Array by Swapping Elements

You are given a 0-indexed array of positive integers nums and a positive integer limit. Your task is to create the lexicographically smallest possible array by performing swaps.

Swap Rules:
• You can swap nums[i] and nums[j] if |nums[i] - nums[j]| ≤ limit
• You can perform any number of swaps

Key Insight: If elements can be swapped transitively (A can swap with B, B can swap with C), then all three elements can be rearranged among their positions.

Example: If nums = [1,5,3,9,4] and limit = 2:
• 1 can swap with 3 (|1-3| = 2 ≤ 2)
• 3 can swap with 5 (|3-5| = 2 ≤ 2)
• 3 can swap with 1 (|3-1| = 2 ≤ 2)
• So positions 0,1,2 can be rearranged optimally: [1,3,5,9,4]

Goal: Find the lexicographically smallest array possible after all valid swaps.

Input & Output

example_1.py — Basic Swapping
$ Input: nums = [1,5,3,9,4], limit = 2
Output: [1,3,5,9,4]
💡 Note: Elements 1, 5, and 3 can all be swapped with each other since |1-3|=2≤2, |5-3|=2≤2. So positions 0,1,2 can be rearranged optimally as [1,3,5]. Elements 9 and 4 cannot swap with others or each other since |9-4|=5>2.
example_2.py — No Swaps Possible
$ Input: nums = [1,7,6,18,2,1], limit = 3
Output: [1,6,7,18,2,1]
💡 Note: Elements that can be swapped: 1 and 2 (|1-2|=1≤3), 7 and 6 (|7-6|=1≤3). Since 1 appears at positions 0 and 5, and there's a 2 at position 4, we can rearrange optimally. Positions 1,2 get values [6,7].
example_3.py — Already Optimal
$ Input: nums = [1,7,28,19,10], limit = 3
Output: [1,7,28,19,10]
💡 Note: No elements can be swapped since all adjacent value differences exceed the limit. The array is already in its lexicographically smallest form.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 109
  • 1 ≤ limit ≤ 109
  • All elements in nums are positive integers

Visualization

Tap to expand
Union-Find Solution VisualizationOriginal Array: [1, 5, 3, 9, 4], limit = 215394pos: 0pos: 1pos: 2pos: 3pos: 4Connected Components (swappable groups):Component 115394IsolatedIsolatedFinal Result: [1, 3, 5, 9, 4]13594Sorted: [1,3,5] → positions [0,1,2]
Understanding the Visualization
1
Identify Swappable Pairs
Find all pairs of elements where |a-b| ≤ limit
2
Build Components
Use Union-Find to group elements that can swap transitively
3
Sort Components
Within each component, sort values and positions separately
4
Optimal Assignment
Assign smallest values to smallest positions in each component
Key Takeaway
🎯 Key Insight: Elements that can be swapped transitively form connected components. By sorting each component optimally, we achieve the lexicographically smallest array in O(n²α(n)) time.
Asked in
Google 35 Meta 28 Amazon 22 Microsoft 18
34.2K Views
Medium Frequency
~25 min Avg. Time
1.5K 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