Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[37,100]
[22,14,33,7]
[18,43,34,16]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3551: Minimum Swaps to Sort by Digit Sum
class Solution {
public int minSwaps(int[] nums) {
int n = nums.length;
int[][] arr = new int[n][2];
for (int i = 0; i < n; i++) {
arr[i][0] = f(nums[i]);
arr[i][1] = nums[i];
}
Arrays.sort(arr, (a, b) -> {
if (a[0] != b[0]) return Integer.compare(a[0], b[0]);
return Integer.compare(a[1], b[1]);
});
Map<Integer, Integer> d = new HashMap<>();
for (int i = 0; i < n; i++) {
d.put(arr[i][1], i);
}
boolean[] vis = new boolean[n];
int ans = n;
for (int i = 0; i < n; i++) {
if (!vis[i]) {
ans--;
int j = i;
while (!vis[j]) {
vis[j] = true;
j = d.get(nums[j]);
}
}
}
return ans;
}
private int f(int x) {
int s = 0;
while (x != 0) {
s += x % 10;
x /= 10;
}
return s;
}
}
// Accepted solution for LeetCode #3551: Minimum Swaps to Sort by Digit Sum
func minSwaps(nums []int) int {
n := len(nums)
arr := make([][2]int, n)
for i := 0; i < n; i++ {
arr[i][0] = f(nums[i])
arr[i][1] = nums[i]
}
sort.Slice(arr, func(i, j int) bool {
if arr[i][0] != arr[j][0] {
return arr[i][0] < arr[j][0]
}
return arr[i][1] < arr[j][1]
})
d := make(map[int]int, n)
for i := 0; i < n; i++ {
d[arr[i][1]] = i
}
vis := make([]bool, n)
ans := n
for i := 0; i < n; i++ {
if !vis[i] {
ans--
j := i
for !vis[j] {
vis[j] = true
j = d[nums[j]]
}
}
}
return ans
}
func f(x int) int {
s := 0
for x != 0 {
s += x % 10
x /= 10
}
return s
}
# Accepted solution for LeetCode #3551: Minimum Swaps to Sort by Digit Sum
class Solution:
def minSwaps(self, nums: List[int]) -> int:
def f(x: int) -> int:
s = 0
while x:
s += x % 10
x //= 10
return s
n = len(nums)
arr = sorted((f(x), x) for x in nums)
d = {a[1]: i for i, a in enumerate(arr)}
ans = n
vis = [False] * n
for i in range(n):
if not vis[i]:
ans -= 1
j = i
while not vis[j]:
vis[j] = True
j = d[nums[j]]
return ans
// Accepted solution for LeetCode #3551: Minimum Swaps to Sort by Digit Sum
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3551: Minimum Swaps to Sort by Digit Sum
// class Solution {
// public int minSwaps(int[] nums) {
// int n = nums.length;
// int[][] arr = new int[n][2];
// for (int i = 0; i < n; i++) {
// arr[i][0] = f(nums[i]);
// arr[i][1] = nums[i];
// }
// Arrays.sort(arr, (a, b) -> {
// if (a[0] != b[0]) return Integer.compare(a[0], b[0]);
// return Integer.compare(a[1], b[1]);
// });
// Map<Integer, Integer> d = new HashMap<>();
// for (int i = 0; i < n; i++) {
// d.put(arr[i][1], i);
// }
// boolean[] vis = new boolean[n];
// int ans = n;
// for (int i = 0; i < n; i++) {
// if (!vis[i]) {
// ans--;
// int j = i;
// while (!vis[j]) {
// vis[j] = true;
// j = d.get(nums[j]);
// }
// }
// }
// return ans;
// }
//
// private int f(int x) {
// int s = 0;
// while (x != 0) {
// s += x % 10;
// x /= 10;
// }
// return s;
// }
// }
// Accepted solution for LeetCode #3551: Minimum Swaps to Sort by Digit Sum
function f(x: number): number {
let s = 0;
while (x !== 0) {
s += x % 10;
x = Math.floor(x / 10);
}
return s;
}
function minSwaps(nums: number[]): number {
const n = nums.length;
const arr: [number, number][] = new Array(n);
for (let i = 0; i < n; i++) {
arr[i] = [f(nums[i]), nums[i]];
}
arr.sort((a, b) => (a[0] !== b[0] ? a[0] - b[0] : a[1] - b[1]));
const d = new Map<number, number>();
for (let i = 0; i < n; i++) {
d.set(arr[i][1], i);
}
const vis: boolean[] = new Array(n).fill(false);
let ans = n;
for (let i = 0; i < n; i++) {
if (!vis[i]) {
ans--;
let j = i;
while (!vis[j]) {
vis[j] = true;
j = d.get(nums[j])!;
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.