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
Constraints
- 1 ≤ nums.length ≤ 103
- 1 ≤ nums[i] ≤ 104
- All integers in nums are distinct
- Each number is a positive integer