AlgoPrecision

Backtracking

Decision Tree Visualizer

Step through choose, explore, reject, and undo moments while the path, board state, result count, time, and space complexity update.

O(C(n,k)) time / O(k) space

Choices

Choice1
Choice2
Choice3
Choice4

Current Path

empty

Results

No result yet

Results

0

Time

O(C(n,k))

Space

O(k)

Step Note

Choose combinations of length 2.

Details
function combinations(values: number[], k: number) {
  const result = []
  function dfs(start: number, path: number[]) {
    if (path.length === k) result.push([...path])
    for (let index = start; index < values.length; index++) {
      path.push(values[index])
      dfs(index + 1, path)
      path.pop()
    }
  }
  return result
}