Pattern visualizer
Combination Sum II
Combination Sum let a value be reused, so the recursive call stayed at the same index. Here every cell is spent once, so the call advances to i + 1 — and that alone is not enough, because the input has repeated values. Picking the first 2 and then the second 2 in a sibling loop branch builds the same multiset twice. Sorting makes equal values neighbours, so the rule is one comparison: inside a single loop, skip a candidate that equals the one before it (i > start), since that value has already been branched on at this level. Taking a duplicate in a DEEPER call is still allowed, which is how [1, 2, 2] is found. Sorting also lets the loop break the moment a candidate exceeds the remaining target. Animated on: candidates = [2, 5, 2, 1, 2], target = 5. Each cell may be used once per combination and the answer must contain no duplicate combinations. Answer: [1, 2, 2], [5]..
Sorted backtracking with a sibling-duplicate skip
candidates = [2, 5, 2, 1, 2], target = 5. Duplicates are scattered, so a search cannot tell "the second 2" from "the first 2" and would report [1, 2, 2] three separate times. Sorting is the fix: equal values become neighbours, and the rule becomes local.
1SORT candidates2FUNCTION backtrack(start, remain, path)3 IF remain = 04 APPEND COPY(path) TO out5 RETURN6 FOR i <- start TO LENGTH(candidates) - 17 IF i > start AND candidates[i] = candidates[i - 1]8 CONTINUE9 IF candidates[i] > remain10 BREAK11 APPEND candidates[i] TO path12 backtrack(i + 1, remain - candidates[i], path)13 REMOVE LAST FROM path14backtrack(0, target, EMPTY LIST)
← / → step · space play · Home restart