Minimum Swaps to Sort by Digit Sum - Problem

You're working with an array of distinct positive integers that needs to be sorted in a special way. Instead of sorting by the actual values, you need to sort by the sum of each number's digits!

Here's the twist: if two numbers have the same digit sum, the smaller number comes first. Your task is to find the minimum number of swaps needed to arrange the array in this special order.

Example: For [321, 45, 123]:
• 321 → digit sum = 3+2+1 = 6
• 45 → digit sum = 4+5 = 9
• 123 → digit sum = 1+2+3 = 6

Since 321 and 123 both have digit sum 6, but 123 < 321, the sorted order should be [123, 321, 45]. This requires 1 swap (swap 321 and 123).

Goal: Return the minimum number of adjacent or non-adjacent swaps to achieve this digit-sum-based sorted order.

Input & Output

example_1.py — Basic Case
$ Input: [321, 45, 123]
Output: 1
💡 Note: Digit sums: 321→6, 45→9, 123→6. Target order: [123, 321, 45]. Need 1 swap to move 321 and 123.
example_2.py — Already Sorted
$ Input: [12, 34, 56]
Output: 0
💡 Note: Digit sums: 12→3, 34→7, 56→11. Array is already sorted by digit sum, so 0 swaps needed.
example_3.py — Multiple Cycles
$ Input: [432, 21, 111, 6]
Output: 2
💡 Note: Digit sums: 432→9, 21→3, 111→3, 6→6. Target: [21, 111, 6, 432]. Two swaps needed: positions form cycles that require 2 swaps total.

Constraints

  • 1 ≤ nums.length ≤ 103
  • 1 ≤ nums[i] ≤ 104
  • All integers in nums are distinct
  • Each number is a positive integer

Visualization

Tap to expand
Minimum Swaps to Sort by Digit SumStep 1: Calculate Digit Sums32164591236Target: [123, 321, 45]Step 2: Position Mapping & Cycle Detection012210Cycle: 0→2→0No cycleKey Insight: Cycle MathematicsCycle Detection Formula• Each cycle of length k needs k-1 swaps• Elements already in correct position: 0 swaps• Our cycle: 0→2→0 (length 2)• Swaps needed: 2-1 = 1• Total minimum swaps = 1Why This Works• Sorting creates a permutation problem• Permutations decompose into disjoint cycles• Each cycle can be sorted optimally• Time: O(n log n), Space: O(n)🎯 Final Answer: 1 swap needed to sort [321, 45, 123] by digit sum
Understanding the Visualization
1
Calculate Digit Sums
For each number, sum its digits to get the sorting key
2
Determine Target Order
Sort by digit sum, with smaller numbers breaking ties
3
Map Positions
Create mapping from current position to target position
4
Find Cycles
Detect cycles in position mapping - each cycle represents elements that need to rotate
5
Count Swaps
Each cycle of length k requires exactly k-1 swaps to resolve
Key Takeaway
🎯 Key Insight: This problem is fundamentally about cycle detection in permutations. By mapping current positions to target positions and counting cycle lengths, we can determine the minimum swaps mathematically rather than through simulation.
Asked in
Google 42 Amazon 38 Microsoft 31 Meta 24
28.4K Views
Medium Frequency
~15 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