Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

candidates =
target =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Each number can be used at most once; avoid duplicate combinations by sorting and skipping duplicates per depth.

Approach

Sort candidates. Backtrack with (start, remaining):

  • Loop i from start.
  • Skip same value if it was already tried at this recursion depth.
  • Choose candidates[i], recurse with i+1 (no reuse), then undo. Stop early when candidate > remaining.

Why it works

Sorting ensures duplicates are adjacent; depth-level skipping prevents generating the same combination in different ways.

Code
Loading...
Complexity
Time: Exponential (output-sensitive)
Space: O(target) recursion depth worst-case
Solution
Updated: 2026-02-23

Idea

Each number can be used at most once; avoid duplicate combinations by sorting and skipping duplicates per depth.

Approach

Sort candidates. Backtrack with (start, remaining):

  • Loop i from start.
  • Skip same value if it was already tried at this recursion depth.
  • Choose candidates[i], recurse with i+1 (no reuse), then undo. Stop early when candidate > remaining.

Why it works

Sorting ensures duplicates are adjacent; depth-level skipping prevents generating the same combination in different ways.

Code
Loading...
Complexity
Time: Exponential (output-sensitive)
Space: O(target) recursion depth worst-case