Minimum Swaps to Sort by Digit Sum - Video Solutions
3551. Minimum Swaps to Sort by Digit Sum | Weekly Contest 450 | Leetcode
Minimum Swaps to Sort by Digit Sum - Video Solution
Watch 6 video solutions for Minimum Swaps to Sort by Digit Sum, a medium level problem involving Array, Hash Table, Sorting. This walkthrough by Rapid Syntax has 1,927 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Problem Statement
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]. Swap37with100to obtain the sorted order. - Thus, the minimum number of swaps required to rearrange
numsis 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
numsis 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]. Swap18with16, and swap43with34to obtain the sorted order. - Thus, the minimum number of swaps required to rearrange
numsis 2.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 109numsconsists of distinct positive integers.
Approach Overview
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.
Complexity Analysis
| 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 |