You are given an array nums of distinct positive integers. You need to sort the array in increasing order based on the sum of the digits of each number. If two numbers have the same digit sum, the smaller number appears first in the sorted order.
Return the minimum number of swaps required to rearrange nums into this sorted order.
A swap is defined as exchanging the values at two distinct positions in the array.
Example 1:
Input: nums = [37,100]
Output: 1
Explanation:
[3 + 7 = 10, 1 + 0 + 0 = 1] → [10, 1][100, 37]. Swap 37 with 100 to obtain the sorted order.nums is 1.Example 2:
Input: nums = [22,14,33,7]
Output: 0
Explanation:
[2 + 2 = 4, 1 + 4 = 5, 3 + 3 = 6, 7 = 7] → [4, 5, 6, 7][22, 14, 33, 7]. The array is already sorted.nums is 0.Example 3:
Input: nums = [18,43,34,16]
Output: 2
Explanation:
[1 + 8 = 9, 4 + 3 = 7, 3 + 4 = 7, 1 + 6 = 7] → [9, 7, 7, 7][16, 34, 43, 18]. Swap 18 with 16, and swap 43 with 34 to obtain the sorted order.nums is 2.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 109nums consists of distinct positive integers.Problem Overview: You are given an array of integers. The array must be reordered so numbers appear in ascending order of their digit sum. The task is to compute the minimum number of swaps required to transform the original array into that sorted order.
Approach 1: Brute Force Swap Simulation (O(n^2) time, O(1) space)
Compute the digit sum for every number, then build the target sorted array based on digit sums. Iterate through the array and whenever the current element is not the element that should appear at that position in the sorted order, find its correct location and swap. Each misplaced element may require scanning the rest of the array to locate its destination, which makes the approach quadratic. This method demonstrates the core idea behind swap counting but becomes slow for large inputs.
Approach 2: Sort + Cycle Detection (O(n log n) time, O(n) space)
Create pairs of (digitSum, value, originalIndex) and sort them by digit sum (and value if needed to maintain deterministic ordering). Once sorted, the problem reduces to computing the minimum swaps required to transform the original index arrangement into the sorted index arrangement. This is done by detecting permutation cycles: if a cycle contains k elements, it requires k - 1 swaps. Iterate through the array, mark visited indices, and count cycle sizes. Sorting contributes O(n log n) time while cycle traversal runs in O(n). This is the standard optimal technique used in many minimum swap problems.
Approach 3: Hash Map Index Tracking (O(n log n) time, O(n) space)
Another implementation keeps a mapping from value (or value plus digit sum) to its index using a hash table. After building the target sorted array using sorting, iterate through positions and swap elements into their correct place while updating the index map. Each swap fixes at least one element, so the number of swaps is minimized. The hash lookup allows constant-time index updates, but the dominant cost remains sorting.
Recommended for interviews: The Sort + Cycle Detection approach is what interviewers typically expect. It shows that you recognize the problem as a permutation cycle problem after sorting by digit sum. Starting with the brute-force swap idea demonstrates understanding, but identifying cycles and counting k - 1 swaps per cycle shows stronger algorithmic maturity. The implementation naturally combines array traversal with sorting and visited tracking.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Swap Simulation | O(n^2) | O(1) | Good for understanding swap mechanics or very small arrays |
| Sort + Cycle Detection | O(n log n) | O(n) | Optimal general solution for minimum swap problems after sorting |
| Hash Map Index Tracking | O(n log n) | O(n) | Useful when implementing direct swap placement with fast index lookup |
3551. Minimum Swaps to Sort by Digit Sum | Weekly Contest 450 | Leetcode • Rapid Syntax • 1,857 views views
Watch 5 more video solutions →Practice Minimum Swaps to Sort by Digit Sum with our built-in code editor and test cases.
Practice on FleetCode