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:
Compute the digit sum for each integer: [3 + 7 = 10, 1 + 0 + 0 = 1] → [10, 1]
Sort the integers based on digit sum: [100, 37]. Swap 37 with 100 to obtain the sorted order.
Thus, the minimum number of swaps required to rearrange nums is 1.
Example 2:
Input:nums = [22,14,33,7]
Output:0
Explanation:
Compute the digit sum for each integer: [2 + 2 = 4, 1 + 4 = 5, 3 + 3 = 6, 7 = 7] → [4, 5, 6, 7]
Sort the integers based on digit sum: [22, 14, 33, 7]. The array is already sorted.
Thus, the minimum number of swaps required to rearrange nums is 0.
Example 3:
Input:nums = [18,43,34,16]
Output:2
Explanation:
Compute the digit sum for each integer: [1 + 8 = 9, 4 + 3 = 7, 3 + 4 = 7, 1 + 6 = 7] → [9, 7, 7, 7]
Sort the integers based on digit sum: [16, 34, 43, 18]. Swap 18 with 16, and swap 43 with 34 to obtain the sorted order.
Thus, the minimum number of swaps required to rearrange nums is 2.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
nums consists of distinct positive integers.
Solutions
Solution 1: Sorting + Permutation Cycles
Thinking
Any two positions may be swapped. Computing the target order and then moving each misplaced value into place is correct on the samples, but \(n\) reaches \(10^5\) and a fresh scan at every step is quadratic. Under arbitrary swaps, the minimum count is given by the cycle decomposition of the permutation from the current array to the target order: a cycle of length \(\ell\) needs \(\ell-1\) swaps, so the total is \(n\) minus the number of cycles. The target order is fixed by \((\textit{digitSum}(x), x)\), so we sort to build a map from each value to its destination index and walk that map cycle by cycle; both the hash table and the traversal order come from the permutation itself.
We first compute the digit sum of each element, pair it with the element, and sort these pairs in ascending order of digit sum. When two digit sums are equal, the smaller value comes first. The sorted order is the target sequence.
Let \(n\) be the length of the array. Treat the original array as a permutation: the value at index \(i\) should move to its index in the target sequence. If this permutation contains \(c\) cycles, the minimum number of swaps is \(n - c\). We store each value's target index in a hash table. Starting from every unvisited index, we repeatedly jump to the index where the current value belongs, until we reach a visited index and thus close one cycle. The answer starts at \(n\) and decreases by one for each cycle.
The time complexity is \(O(n \log n)\), and the space complexity is \(O(n)\), where \(n\) is the length of \(\textit{nums}\).