← Back

Backtracking (Subsets)

The choose / explore / un-choose template behind subsets, permutations, and combinations.

1 / 1
func subsets(nums []int) [][]int {

}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res = append(res, append([]int{}, path...))
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res = append(res, append([]int{}, path...))

        // Try each remaining element as the next one to include.
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res = append(res, append([]int{}, path...))

        // Try each remaining element as the next one to include.
        for i := start; i < len(nums); i++ {
        }
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res = append(res, append([]int{}, path...))

        // Try each remaining element as the next one to include.
        for i := start; i < len(nums); i++ {
            // CHOOSE: add nums[i] to the current subset.
        }
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res = append(res, append([]int{}, path...))

        // Try each remaining element as the next one to include.
        for i := start; i < len(nums); i++ {
            // CHOOSE: add nums[i] to the current subset.
            path = append(path, nums[i])
        }
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res = append(res, append([]int{}, path...))

        // Try each remaining element as the next one to include.
        for i := start; i < len(nums); i++ {
            // CHOOSE: add nums[i] to the current subset.
            path = append(path, nums[i])

            // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
        }
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res = append(res, append([]int{}, path...))

        // Try each remaining element as the next one to include.
        for i := start; i < len(nums); i++ {
            // CHOOSE: add nums[i] to the current subset.
            path = append(path, nums[i])

            // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            backtrack(i + 1)
        }
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res = append(res, append([]int{}, path...))

        // Try each remaining element as the next one to include.
        for i := start; i < len(nums); i++ {
            // CHOOSE: add nums[i] to the current subset.
            path = append(path, nums[i])

            // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            backtrack(i + 1)

            // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
        }
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res = append(res, append([]int{}, path...))

        // Try each remaining element as the next one to include.
        for i := start; i < len(nums); i++ {
            // CHOOSE: add nums[i] to the current subset.
            path = append(path, nums[i])

            // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            backtrack(i + 1)

            // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
            path = path[:len(path)-1]
        }
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res = append(res, append([]int{}, path...))

        // Try each remaining element as the next one to include.
        for i := start; i < len(nums); i++ {
            // CHOOSE: add nums[i] to the current subset.
            path = append(path, nums[i])

            // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            backtrack(i + 1)

            // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
            path = path[:len(path)-1]
        }
    }

    backtrack(0) // kick off from the empty subset
    return res
}
def subsets(nums):
    pass
def subsets(nums):
    # res collects finished subsets; path is the one we're currently building.
    pass
def subsets(nums):
    # res collects finished subsets; path is the one we're currently building.
    res = []
    path = []
def subsets(nums):
    # res collects finished subsets; path is the one we're currently building.
    res = []
    path = []

    # backtrack explores every subset that draws from index `start` onward.
def subsets(nums):
    # res collects finished subsets; path is the one we're currently building.
    res = []
    path = []

    # backtrack explores every subset that draws from index `start` onward.
    def backtrack(start):
        pass
def subsets(nums):
    # res collects finished subsets; path is the one we're currently building.
    res = []
    path = []

    # backtrack explores every subset that draws from index `start` onward.
    def backtrack(start):
        # Every node of the recursion is itself a valid subset. Store a COPY,
        # because path keeps mutating under us.
        pass
def subsets(nums):
    # res collects finished subsets; path is the one we're currently building.
    res = []
    path = []

    # backtrack explores every subset that draws from index `start` onward.
    def backtrack(start):
        # Every node of the recursion is itself a valid subset. Store a COPY,
        # because path keeps mutating under us.
        res.append(path[:])
def subsets(nums):
    # res collects finished subsets; path is the one we're currently building.
    res = []
    path = []

    # backtrack explores every subset that draws from index `start` onward.
    def backtrack(start):
        # Every node of the recursion is itself a valid subset. Store a COPY,
        # because path keeps mutating under us.
        res.append(path[:])

        # Try each remaining element as the next one to include.
def subsets(nums):
    # res collects finished subsets; path is the one we're currently building.
    res = []
    path = []

    # backtrack explores every subset that draws from index `start` onward.
    def backtrack(start):
        # Every node of the recursion is itself a valid subset. Store a COPY,
        # because path keeps mutating under us.
        res.append(path[:])

        # Try each remaining element as the next one to include.
        for i in range(start, len(nums)):
            pass
def subsets(nums):
    # res collects finished subsets; path is the one we're currently building.
    res = []
    path = []

    # backtrack explores every subset that draws from index `start` onward.
    def backtrack(start):
        # Every node of the recursion is itself a valid subset. Store a COPY,
        # because path keeps mutating under us.
        res.append(path[:])

        # Try each remaining element as the next one to include.
        for i in range(start, len(nums)):
            # CHOOSE: add nums[i] to the current subset.
            pass
def subsets(nums):
    # res collects finished subsets; path is the one we're currently building.
    res = []
    path = []

    # backtrack explores every subset that draws from index `start` onward.
    def backtrack(start):
        # Every node of the recursion is itself a valid subset. Store a COPY,
        # because path keeps mutating under us.
        res.append(path[:])

        # Try each remaining element as the next one to include.
        for i in range(start, len(nums)):
            # CHOOSE: add nums[i] to the current subset.
            path.append(nums[i])
def subsets(nums):
    # res collects finished subsets; path is the one we're currently building.
    res = []
    path = []

    # backtrack explores every subset that draws from index `start` onward.
    def backtrack(start):
        # Every node of the recursion is itself a valid subset. Store a COPY,
        # because path keeps mutating under us.
        res.append(path[:])

        # Try each remaining element as the next one to include.
        for i in range(start, len(nums)):
            # CHOOSE: add nums[i] to the current subset.
            path.append(nums[i])

            # EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
def subsets(nums):
    # res collects finished subsets; path is the one we're currently building.
    res = []
    path = []

    # backtrack explores every subset that draws from index `start` onward.
    def backtrack(start):
        # Every node of the recursion is itself a valid subset. Store a COPY,
        # because path keeps mutating under us.
        res.append(path[:])

        # Try each remaining element as the next one to include.
        for i in range(start, len(nums)):
            # CHOOSE: add nums[i] to the current subset.
            path.append(nums[i])

            # EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            backtrack(i + 1)
def subsets(nums):
    # res collects finished subsets; path is the one we're currently building.
    res = []
    path = []

    # backtrack explores every subset that draws from index `start` onward.
    def backtrack(start):
        # Every node of the recursion is itself a valid subset. Store a COPY,
        # because path keeps mutating under us.
        res.append(path[:])

        # Try each remaining element as the next one to include.
        for i in range(start, len(nums)):
            # CHOOSE: add nums[i] to the current subset.
            path.append(nums[i])

            # EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            backtrack(i + 1)

            # UN-CHOOSE: remove nums[i] so the next iteration starts clean.
def subsets(nums):
    # res collects finished subsets; path is the one we're currently building.
    res = []
    path = []

    # backtrack explores every subset that draws from index `start` onward.
    def backtrack(start):
        # Every node of the recursion is itself a valid subset. Store a COPY,
        # because path keeps mutating under us.
        res.append(path[:])

        # Try each remaining element as the next one to include.
        for i in range(start, len(nums)):
            # CHOOSE: add nums[i] to the current subset.
            path.append(nums[i])

            # EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            backtrack(i + 1)

            # UN-CHOOSE: remove nums[i] so the next iteration starts clean.
            path.pop()
def subsets(nums):
    # res collects finished subsets; path is the one we're currently building.
    res = []
    path = []

    # backtrack explores every subset that draws from index `start` onward.
    def backtrack(start):
        # Every node of the recursion is itself a valid subset. Store a COPY,
        # because path keeps mutating under us.
        res.append(path[:])

        # Try each remaining element as the next one to include.
        for i in range(start, len(nums)):
            # CHOOSE: add nums[i] to the current subset.
            path.append(nums[i])

            # EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            backtrack(i + 1)

            # UN-CHOOSE: remove nums[i] so the next iteration starts clean.
            path.pop()

    backtrack(0)  # kick off from the empty subset
    return res
function subsets(nums) {

}
function subsets(nums) {
  // res collects finished subsets; path is the one we're currently building.
}
function subsets(nums) {
  // res collects finished subsets; path is the one we're currently building.
  const res = [];
  const path = [];
}
function subsets(nums) {
  // res collects finished subsets; path is the one we're currently building.
  const res = [];
  const path = [];

  // backtrack explores every subset that draws from index `start` onward.
}
function subsets(nums) {
  // res collects finished subsets; path is the one we're currently building.
  const res = [];
  const path = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start) {
  }
}
function subsets(nums) {
  // res collects finished subsets; path is the one we're currently building.
  const res = [];
  const path = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
  }
}
function subsets(nums) {
  // res collects finished subsets; path is the one we're currently building.
  const res = [];
  const path = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    res.push([...path]);
  }
}
function subsets(nums) {
  // res collects finished subsets; path is the one we're currently building.
  const res = [];
  const path = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    res.push([...path]);

    // Try each remaining element as the next one to include.
  }
}
function subsets(nums) {
  // res collects finished subsets; path is the one we're currently building.
  const res = [];
  const path = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    res.push([...path]);

    // Try each remaining element as the next one to include.
    for (let i = start; i < nums.length; i++) {
    }
  }
}
function subsets(nums) {
  // res collects finished subsets; path is the one we're currently building.
  const res = [];
  const path = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    res.push([...path]);

    // Try each remaining element as the next one to include.
    for (let i = start; i < nums.length; i++) {
      // CHOOSE: add nums[i] to the current subset.
    }
  }
}
function subsets(nums) {
  // res collects finished subsets; path is the one we're currently building.
  const res = [];
  const path = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    res.push([...path]);

    // Try each remaining element as the next one to include.
    for (let i = start; i < nums.length; i++) {
      // CHOOSE: add nums[i] to the current subset.
      path.push(nums[i]);
    }
  }
}
function subsets(nums) {
  // res collects finished subsets; path is the one we're currently building.
  const res = [];
  const path = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    res.push([...path]);

    // Try each remaining element as the next one to include.
    for (let i = start; i < nums.length; i++) {
      // CHOOSE: add nums[i] to the current subset.
      path.push(nums[i]);

      // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
    }
  }
}
function subsets(nums) {
  // res collects finished subsets; path is the one we're currently building.
  const res = [];
  const path = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    res.push([...path]);

    // Try each remaining element as the next one to include.
    for (let i = start; i < nums.length; i++) {
      // CHOOSE: add nums[i] to the current subset.
      path.push(nums[i]);

      // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
      backtrack(i + 1);
    }
  }
}
function subsets(nums) {
  // res collects finished subsets; path is the one we're currently building.
  const res = [];
  const path = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    res.push([...path]);

    // Try each remaining element as the next one to include.
    for (let i = start; i < nums.length; i++) {
      // CHOOSE: add nums[i] to the current subset.
      path.push(nums[i]);

      // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
      backtrack(i + 1);

      // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
    }
  }
}
function subsets(nums) {
  // res collects finished subsets; path is the one we're currently building.
  const res = [];
  const path = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    res.push([...path]);

    // Try each remaining element as the next one to include.
    for (let i = start; i < nums.length; i++) {
      // CHOOSE: add nums[i] to the current subset.
      path.push(nums[i]);

      // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
      backtrack(i + 1);

      // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
      path.pop();
    }
  }
}
function subsets(nums) {
  // res collects finished subsets; path is the one we're currently building.
  const res = [];
  const path = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    res.push([...path]);

    // Try each remaining element as the next one to include.
    for (let i = start; i < nums.length; i++) {
      // CHOOSE: add nums[i] to the current subset.
      path.push(nums[i]);

      // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
      backtrack(i + 1);

      // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
      path.pop();
    }
  }

  backtrack(0); // kick off from the empty subset
  return res;
}
function subsets(nums: number[]): number[][] {

}
function subsets(nums: number[]): number[][] {
  // res collects finished subsets; path is the one we're currently building.
}
function subsets(nums: number[]): number[][] {
  // res collects finished subsets; path is the one we're currently building.
  const res: number[][] = [];
  const path: number[] = [];
}
function subsets(nums: number[]): number[][] {
  // res collects finished subsets; path is the one we're currently building.
  const res: number[][] = [];
  const path: number[] = [];

  // backtrack explores every subset that draws from index `start` onward.
}
function subsets(nums: number[]): number[][] {
  // res collects finished subsets; path is the one we're currently building.
  const res: number[][] = [];
  const path: number[] = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start: number): void {
  }
}
function subsets(nums: number[]): number[][] {
  // res collects finished subsets; path is the one we're currently building.
  const res: number[][] = [];
  const path: number[] = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start: number): void {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
  }
}
function subsets(nums: number[]): number[][] {
  // res collects finished subsets; path is the one we're currently building.
  const res: number[][] = [];
  const path: number[] = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start: number): void {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    res.push([...path]);
  }
}
function subsets(nums: number[]): number[][] {
  // res collects finished subsets; path is the one we're currently building.
  const res: number[][] = [];
  const path: number[] = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start: number): void {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    res.push([...path]);

    // Try each remaining element as the next one to include.
  }
}
function subsets(nums: number[]): number[][] {
  // res collects finished subsets; path is the one we're currently building.
  const res: number[][] = [];
  const path: number[] = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start: number): void {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    res.push([...path]);

    // Try each remaining element as the next one to include.
    for (let i = start; i < nums.length; i++) {
    }
  }
}
function subsets(nums: number[]): number[][] {
  // res collects finished subsets; path is the one we're currently building.
  const res: number[][] = [];
  const path: number[] = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start: number): void {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    res.push([...path]);

    // Try each remaining element as the next one to include.
    for (let i = start; i < nums.length; i++) {
      // CHOOSE: add nums[i] to the current subset.
    }
  }
}
function subsets(nums: number[]): number[][] {
  // res collects finished subsets; path is the one we're currently building.
  const res: number[][] = [];
  const path: number[] = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start: number): void {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    res.push([...path]);

    // Try each remaining element as the next one to include.
    for (let i = start; i < nums.length; i++) {
      // CHOOSE: add nums[i] to the current subset.
      path.push(nums[i]);
    }
  }
}
function subsets(nums: number[]): number[][] {
  // res collects finished subsets; path is the one we're currently building.
  const res: number[][] = [];
  const path: number[] = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start: number): void {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    res.push([...path]);

    // Try each remaining element as the next one to include.
    for (let i = start; i < nums.length; i++) {
      // CHOOSE: add nums[i] to the current subset.
      path.push(nums[i]);

      // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
    }
  }
}
function subsets(nums: number[]): number[][] {
  // res collects finished subsets; path is the one we're currently building.
  const res: number[][] = [];
  const path: number[] = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start: number): void {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    res.push([...path]);

    // Try each remaining element as the next one to include.
    for (let i = start; i < nums.length; i++) {
      // CHOOSE: add nums[i] to the current subset.
      path.push(nums[i]);

      // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
      backtrack(i + 1);
    }
  }
}
function subsets(nums: number[]): number[][] {
  // res collects finished subsets; path is the one we're currently building.
  const res: number[][] = [];
  const path: number[] = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start: number): void {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    res.push([...path]);

    // Try each remaining element as the next one to include.
    for (let i = start; i < nums.length; i++) {
      // CHOOSE: add nums[i] to the current subset.
      path.push(nums[i]);

      // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
      backtrack(i + 1);

      // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
    }
  }
}
function subsets(nums: number[]): number[][] {
  // res collects finished subsets; path is the one we're currently building.
  const res: number[][] = [];
  const path: number[] = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start: number): void {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    res.push([...path]);

    // Try each remaining element as the next one to include.
    for (let i = start; i < nums.length; i++) {
      // CHOOSE: add nums[i] to the current subset.
      path.push(nums[i]);

      // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
      backtrack(i + 1);

      // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
      path.pop();
    }
  }
}
function subsets(nums: number[]): number[][] {
  // res collects finished subsets; path is the one we're currently building.
  const res: number[][] = [];
  const path: number[] = [];

  // backtrack explores every subset that draws from index `start` onward.
  function backtrack(start: number): void {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    res.push([...path]);

    // Try each remaining element as the next one to include.
    for (let i = start; i < nums.length; i++) {
      // CHOOSE: add nums[i] to the current subset.
      path.push(nums[i]);

      // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
      backtrack(i + 1);

      // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
      path.pop();
    }
  }

  backtrack(0); // kick off from the empty subset
  return res;
}
import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<List<Integer>> subsets(int[] nums) {

    }
}
import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
    }
}
import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
    }
}
import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
    }

    // backtrack explores every subset that draws from index `start` onward.
}
import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
    }

    // backtrack explores every subset that draws from index `start` onward.
    private void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> res) {
    }
}
import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
    }

    // backtrack explores every subset that draws from index `start` onward.
    private void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
    }
}
import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
    }

    // backtrack explores every subset that draws from index `start` onward.
    private void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res.add(new ArrayList<>(path));
    }
}
import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
    }

    // backtrack explores every subset that draws from index `start` onward.
    private void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res.add(new ArrayList<>(path));

        // Try each remaining element as the next one to include.
    }
}
import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
    }

    // backtrack explores every subset that draws from index `start` onward.
    private void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res.add(new ArrayList<>(path));

        // Try each remaining element as the next one to include.
        for (int i = start; i < nums.length; i++) {
        }
    }
}
import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
    }

    // backtrack explores every subset that draws from index `start` onward.
    private void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res.add(new ArrayList<>(path));

        // Try each remaining element as the next one to include.
        for (int i = start; i < nums.length; i++) {
            // CHOOSE: add nums[i] to the current subset.
        }
    }
}
import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
    }

    // backtrack explores every subset that draws from index `start` onward.
    private void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res.add(new ArrayList<>(path));

        // Try each remaining element as the next one to include.
        for (int i = start; i < nums.length; i++) {
            // CHOOSE: add nums[i] to the current subset.
            path.add(nums[i]);
        }
    }
}
import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
    }

    // backtrack explores every subset that draws from index `start` onward.
    private void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res.add(new ArrayList<>(path));

        // Try each remaining element as the next one to include.
        for (int i = start; i < nums.length; i++) {
            // CHOOSE: add nums[i] to the current subset.
            path.add(nums[i]);

            // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
        }
    }
}
import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
    }

    // backtrack explores every subset that draws from index `start` onward.
    private void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res.add(new ArrayList<>(path));

        // Try each remaining element as the next one to include.
        for (int i = start; i < nums.length; i++) {
            // CHOOSE: add nums[i] to the current subset.
            path.add(nums[i]);

            // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            backtrack(nums, i + 1, path, res);
        }
    }
}
import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
    }

    // backtrack explores every subset that draws from index `start` onward.
    private void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res.add(new ArrayList<>(path));

        // Try each remaining element as the next one to include.
        for (int i = start; i < nums.length; i++) {
            // CHOOSE: add nums[i] to the current subset.
            path.add(nums[i]);

            // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            backtrack(nums, i + 1, path, res);

            // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
        }
    }
}
import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
    }

    // backtrack explores every subset that draws from index `start` onward.
    private void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res.add(new ArrayList<>(path));

        // Try each remaining element as the next one to include.
        for (int i = start; i < nums.length; i++) {
            // CHOOSE: add nums[i] to the current subset.
            path.add(nums[i]);

            // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            backtrack(nums, i + 1, path, res);

            // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
            path.remove(path.size() - 1);
        }
    }
}
import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();

        backtrack(nums, 0, path, res); // kick off from the empty subset
        return res;
    }

    // backtrack explores every subset that draws from index `start` onward.
    private void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res.add(new ArrayList<>(path));

        // Try each remaining element as the next one to include.
        for (int i = start; i < nums.length; i++) {
            // CHOOSE: add nums[i] to the current subset.
            path.add(nums[i]);

            // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            backtrack(nums, i + 1, path, res);

            // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
            path.remove(path.size() - 1);
        }
    }
}
using System.Collections.Generic;

public class Solution {
    public IList<IList<int>> Subsets(int[] nums) {

    }
}
using System.Collections.Generic;

public class Solution {
    public IList<IList<int>> Subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
    }
}
using System.Collections.Generic;

public class Solution {
    public IList<IList<int>> Subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<IList<int>> res = new List<IList<int>>();
        List<int> path = new List<int>();
    }
}
using System.Collections.Generic;

public class Solution {
    public IList<IList<int>> Subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<IList<int>> res = new List<IList<int>>();
        List<int> path = new List<int>();

        // Backtrack explores every subset that draws from index `start` onward.
    }
}
using System.Collections.Generic;

public class Solution {
    public IList<IList<int>> Subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<IList<int>> res = new List<IList<int>>();
        List<int> path = new List<int>();

        // Backtrack explores every subset that draws from index `start` onward.
        void Backtrack(int start) {
        }
    }
}
using System.Collections.Generic;

public class Solution {
    public IList<IList<int>> Subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<IList<int>> res = new List<IList<int>>();
        List<int> path = new List<int>();

        // Backtrack explores every subset that draws from index `start` onward.
        void Backtrack(int start) {
            // Every node of the recursion is itself a valid subset. Store a COPY,
            // because path keeps mutating under us.
        }
    }
}
using System.Collections.Generic;

public class Solution {
    public IList<IList<int>> Subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<IList<int>> res = new List<IList<int>>();
        List<int> path = new List<int>();

        // Backtrack explores every subset that draws from index `start` onward.
        void Backtrack(int start) {
            // Every node of the recursion is itself a valid subset. Store a COPY,
            // because path keeps mutating under us.
            res.Add(new List<int>(path));
        }
    }
}
using System.Collections.Generic;

public class Solution {
    public IList<IList<int>> Subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<IList<int>> res = new List<IList<int>>();
        List<int> path = new List<int>();

        // Backtrack explores every subset that draws from index `start` onward.
        void Backtrack(int start) {
            // Every node of the recursion is itself a valid subset. Store a COPY,
            // because path keeps mutating under us.
            res.Add(new List<int>(path));

            // Try each remaining element as the next one to include.
        }
    }
}
using System.Collections.Generic;

public class Solution {
    public IList<IList<int>> Subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<IList<int>> res = new List<IList<int>>();
        List<int> path = new List<int>();

        // Backtrack explores every subset that draws from index `start` onward.
        void Backtrack(int start) {
            // Every node of the recursion is itself a valid subset. Store a COPY,
            // because path keeps mutating under us.
            res.Add(new List<int>(path));

            // Try each remaining element as the next one to include.
            for (int i = start; i < nums.Length; i++) {
            }
        }
    }
}
using System.Collections.Generic;

public class Solution {
    public IList<IList<int>> Subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<IList<int>> res = new List<IList<int>>();
        List<int> path = new List<int>();

        // Backtrack explores every subset that draws from index `start` onward.
        void Backtrack(int start) {
            // Every node of the recursion is itself a valid subset. Store a COPY,
            // because path keeps mutating under us.
            res.Add(new List<int>(path));

            // Try each remaining element as the next one to include.
            for (int i = start; i < nums.Length; i++) {
                // CHOOSE: add nums[i] to the current subset.
            }
        }
    }
}
using System.Collections.Generic;

public class Solution {
    public IList<IList<int>> Subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<IList<int>> res = new List<IList<int>>();
        List<int> path = new List<int>();

        // Backtrack explores every subset that draws from index `start` onward.
        void Backtrack(int start) {
            // Every node of the recursion is itself a valid subset. Store a COPY,
            // because path keeps mutating under us.
            res.Add(new List<int>(path));

            // Try each remaining element as the next one to include.
            for (int i = start; i < nums.Length; i++) {
                // CHOOSE: add nums[i] to the current subset.
                path.Add(nums[i]);
            }
        }
    }
}
using System.Collections.Generic;

public class Solution {
    public IList<IList<int>> Subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<IList<int>> res = new List<IList<int>>();
        List<int> path = new List<int>();

        // Backtrack explores every subset that draws from index `start` onward.
        void Backtrack(int start) {
            // Every node of the recursion is itself a valid subset. Store a COPY,
            // because path keeps mutating under us.
            res.Add(new List<int>(path));

            // Try each remaining element as the next one to include.
            for (int i = start; i < nums.Length; i++) {
                // CHOOSE: add nums[i] to the current subset.
                path.Add(nums[i]);

                // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            }
        }
    }
}
using System.Collections.Generic;

public class Solution {
    public IList<IList<int>> Subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<IList<int>> res = new List<IList<int>>();
        List<int> path = new List<int>();

        // Backtrack explores every subset that draws from index `start` onward.
        void Backtrack(int start) {
            // Every node of the recursion is itself a valid subset. Store a COPY,
            // because path keeps mutating under us.
            res.Add(new List<int>(path));

            // Try each remaining element as the next one to include.
            for (int i = start; i < nums.Length; i++) {
                // CHOOSE: add nums[i] to the current subset.
                path.Add(nums[i]);

                // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
                Backtrack(i + 1);
            }
        }
    }
}
using System.Collections.Generic;

public class Solution {
    public IList<IList<int>> Subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<IList<int>> res = new List<IList<int>>();
        List<int> path = new List<int>();

        // Backtrack explores every subset that draws from index `start` onward.
        void Backtrack(int start) {
            // Every node of the recursion is itself a valid subset. Store a COPY,
            // because path keeps mutating under us.
            res.Add(new List<int>(path));

            // Try each remaining element as the next one to include.
            for (int i = start; i < nums.Length; i++) {
                // CHOOSE: add nums[i] to the current subset.
                path.Add(nums[i]);

                // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
                Backtrack(i + 1);

                // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
            }
        }
    }
}
using System.Collections.Generic;

public class Solution {
    public IList<IList<int>> Subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<IList<int>> res = new List<IList<int>>();
        List<int> path = new List<int>();

        // Backtrack explores every subset that draws from index `start` onward.
        void Backtrack(int start) {
            // Every node of the recursion is itself a valid subset. Store a COPY,
            // because path keeps mutating under us.
            res.Add(new List<int>(path));

            // Try each remaining element as the next one to include.
            for (int i = start; i < nums.Length; i++) {
                // CHOOSE: add nums[i] to the current subset.
                path.Add(nums[i]);

                // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
                Backtrack(i + 1);

                // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
                path.RemoveAt(path.Count - 1);
            }
        }
    }
}
using System.Collections.Generic;

public class Solution {
    public IList<IList<int>> Subsets(int[] nums) {
        // res collects finished subsets; path is the one we're currently building.
        List<IList<int>> res = new List<IList<int>>();
        List<int> path = new List<int>();

        // Backtrack explores every subset that draws from index `start` onward.
        void Backtrack(int start) {
            // Every node of the recursion is itself a valid subset. Store a COPY,
            // because path keeps mutating under us.
            res.Add(new List<int>(path));

            // Try each remaining element as the next one to include.
            for (int i = start; i < nums.Length; i++) {
                // CHOOSE: add nums[i] to the current subset.
                path.Add(nums[i]);

                // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
                Backtrack(i + 1);

                // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
                path.RemoveAt(path.Count - 1);
            }
        }

        Backtrack(0); // kick off from the empty subset
        return res;
    }
}
#include <stdlib.h>
#include <string.h>

// Declared up front so subsets can call it before it is defined further down.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes);

// The caller owns every returned row, the row array and *returnColumnSizes.
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {

}
#include <stdlib.h>
#include <string.h>

// Declared up front so subsets can call it before it is defined further down.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes);

// The caller owns every returned row, the row array and *returnColumnSizes.
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
    // res collects finished subsets; path is the one we're currently building.
}
#include <stdlib.h>
#include <string.h>

// Declared up front so subsets can call it before it is defined further down.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes);

// The caller owns every returned row, the row array and *returnColumnSizes.
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
    // res collects finished subsets; path is the one we're currently building.
    int total = 1 << numsSize; // there are exactly 2^n subsets, so size it up front
    int** res = malloc((size_t)total * sizeof(int*));
    int* colSizes = malloc((size_t)total * sizeof(int));
    int* path = malloc((size_t)(numsSize + 1) * sizeof(int));
    int resSize = 0, pathSize = 0;
}
#include <stdlib.h>
#include <string.h>

// Declared up front so subsets can call it before it is defined further down.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes);

// The caller owns every returned row, the row array and *returnColumnSizes.
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
    // res collects finished subsets; path is the one we're currently building.
    int total = 1 << numsSize; // there are exactly 2^n subsets, so size it up front
    int** res = malloc((size_t)total * sizeof(int*));
    int* colSizes = malloc((size_t)total * sizeof(int));
    int* path = malloc((size_t)(numsSize + 1) * sizeof(int));
    int resSize = 0, pathSize = 0;
}

// backtrack explores every subset that draws from index `start` onward.
#include <stdlib.h>
#include <string.h>

// Declared up front so subsets can call it before it is defined further down.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes);

// The caller owns every returned row, the row array and *returnColumnSizes.
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
    // res collects finished subsets; path is the one we're currently building.
    int total = 1 << numsSize; // there are exactly 2^n subsets, so size it up front
    int** res = malloc((size_t)total * sizeof(int*));
    int* colSizes = malloc((size_t)total * sizeof(int));
    int* path = malloc((size_t)(numsSize + 1) * sizeof(int));
    int resSize = 0, pathSize = 0;
}

// backtrack explores every subset that draws from index `start` onward.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes) {
}
#include <stdlib.h>
#include <string.h>

// Declared up front so subsets can call it before it is defined further down.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes);

// The caller owns every returned row, the row array and *returnColumnSizes.
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
    // res collects finished subsets; path is the one we're currently building.
    int total = 1 << numsSize; // there are exactly 2^n subsets, so size it up front
    int** res = malloc((size_t)total * sizeof(int*));
    int* colSizes = malloc((size_t)total * sizeof(int));
    int* path = malloc((size_t)(numsSize + 1) * sizeof(int));
    int resSize = 0, pathSize = 0;
}

// backtrack explores every subset that draws from index `start` onward.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
}
#include <stdlib.h>
#include <string.h>

// Declared up front so subsets can call it before it is defined further down.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes);

// The caller owns every returned row, the row array and *returnColumnSizes.
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
    // res collects finished subsets; path is the one we're currently building.
    int total = 1 << numsSize; // there are exactly 2^n subsets, so size it up front
    int** res = malloc((size_t)total * sizeof(int*));
    int* colSizes = malloc((size_t)total * sizeof(int));
    int* path = malloc((size_t)(numsSize + 1) * sizeof(int));
    int resSize = 0, pathSize = 0;
}

// backtrack explores every subset that draws from index `start` onward.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    int* copy = malloc((size_t)(*pathSize + 1) * sizeof(int)); // +1: malloc(0) is not portable
    memcpy(copy, path, (size_t)*pathSize * sizeof(int));
    colSizes[*resSize] = *pathSize;
    res[(*resSize)++] = copy;
}
#include <stdlib.h>
#include <string.h>

// Declared up front so subsets can call it before it is defined further down.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes);

// The caller owns every returned row, the row array and *returnColumnSizes.
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
    // res collects finished subsets; path is the one we're currently building.
    int total = 1 << numsSize; // there are exactly 2^n subsets, so size it up front
    int** res = malloc((size_t)total * sizeof(int*));
    int* colSizes = malloc((size_t)total * sizeof(int));
    int* path = malloc((size_t)(numsSize + 1) * sizeof(int));
    int resSize = 0, pathSize = 0;
}

// backtrack explores every subset that draws from index `start` onward.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    int* copy = malloc((size_t)(*pathSize + 1) * sizeof(int)); // +1: malloc(0) is not portable
    memcpy(copy, path, (size_t)*pathSize * sizeof(int));
    colSizes[*resSize] = *pathSize;
    res[(*resSize)++] = copy;

    // Try each remaining element as the next one to include.
}
#include <stdlib.h>
#include <string.h>

// Declared up front so subsets can call it before it is defined further down.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes);

// The caller owns every returned row, the row array and *returnColumnSizes.
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
    // res collects finished subsets; path is the one we're currently building.
    int total = 1 << numsSize; // there are exactly 2^n subsets, so size it up front
    int** res = malloc((size_t)total * sizeof(int*));
    int* colSizes = malloc((size_t)total * sizeof(int));
    int* path = malloc((size_t)(numsSize + 1) * sizeof(int));
    int resSize = 0, pathSize = 0;
}

// backtrack explores every subset that draws from index `start` onward.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    int* copy = malloc((size_t)(*pathSize + 1) * sizeof(int)); // +1: malloc(0) is not portable
    memcpy(copy, path, (size_t)*pathSize * sizeof(int));
    colSizes[*resSize] = *pathSize;
    res[(*resSize)++] = copy;

    // Try each remaining element as the next one to include.
    for (int i = start; i < numsSize; i++) {
    }
}
#include <stdlib.h>
#include <string.h>

// Declared up front so subsets can call it before it is defined further down.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes);

// The caller owns every returned row, the row array and *returnColumnSizes.
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
    // res collects finished subsets; path is the one we're currently building.
    int total = 1 << numsSize; // there are exactly 2^n subsets, so size it up front
    int** res = malloc((size_t)total * sizeof(int*));
    int* colSizes = malloc((size_t)total * sizeof(int));
    int* path = malloc((size_t)(numsSize + 1) * sizeof(int));
    int resSize = 0, pathSize = 0;
}

// backtrack explores every subset that draws from index `start` onward.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    int* copy = malloc((size_t)(*pathSize + 1) * sizeof(int)); // +1: malloc(0) is not portable
    memcpy(copy, path, (size_t)*pathSize * sizeof(int));
    colSizes[*resSize] = *pathSize;
    res[(*resSize)++] = copy;

    // Try each remaining element as the next one to include.
    for (int i = start; i < numsSize; i++) {
        // CHOOSE: add nums[i] to the current subset.
    }
}
#include <stdlib.h>
#include <string.h>

// Declared up front so subsets can call it before it is defined further down.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes);

// The caller owns every returned row, the row array and *returnColumnSizes.
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
    // res collects finished subsets; path is the one we're currently building.
    int total = 1 << numsSize; // there are exactly 2^n subsets, so size it up front
    int** res = malloc((size_t)total * sizeof(int*));
    int* colSizes = malloc((size_t)total * sizeof(int));
    int* path = malloc((size_t)(numsSize + 1) * sizeof(int));
    int resSize = 0, pathSize = 0;
}

// backtrack explores every subset that draws from index `start` onward.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    int* copy = malloc((size_t)(*pathSize + 1) * sizeof(int)); // +1: malloc(0) is not portable
    memcpy(copy, path, (size_t)*pathSize * sizeof(int));
    colSizes[*resSize] = *pathSize;
    res[(*resSize)++] = copy;

    // Try each remaining element as the next one to include.
    for (int i = start; i < numsSize; i++) {
        // CHOOSE: add nums[i] to the current subset.
        path[(*pathSize)++] = nums[i];
    }
}
#include <stdlib.h>
#include <string.h>

// Declared up front so subsets can call it before it is defined further down.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes);

// The caller owns every returned row, the row array and *returnColumnSizes.
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
    // res collects finished subsets; path is the one we're currently building.
    int total = 1 << numsSize; // there are exactly 2^n subsets, so size it up front
    int** res = malloc((size_t)total * sizeof(int*));
    int* colSizes = malloc((size_t)total * sizeof(int));
    int* path = malloc((size_t)(numsSize + 1) * sizeof(int));
    int resSize = 0, pathSize = 0;
}

// backtrack explores every subset that draws from index `start` onward.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    int* copy = malloc((size_t)(*pathSize + 1) * sizeof(int)); // +1: malloc(0) is not portable
    memcpy(copy, path, (size_t)*pathSize * sizeof(int));
    colSizes[*resSize] = *pathSize;
    res[(*resSize)++] = copy;

    // Try each remaining element as the next one to include.
    for (int i = start; i < numsSize; i++) {
        // CHOOSE: add nums[i] to the current subset.
        path[(*pathSize)++] = nums[i];

        // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
    }
}
#include <stdlib.h>
#include <string.h>

// Declared up front so subsets can call it before it is defined further down.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes);

// The caller owns every returned row, the row array and *returnColumnSizes.
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
    // res collects finished subsets; path is the one we're currently building.
    int total = 1 << numsSize; // there are exactly 2^n subsets, so size it up front
    int** res = malloc((size_t)total * sizeof(int*));
    int* colSizes = malloc((size_t)total * sizeof(int));
    int* path = malloc((size_t)(numsSize + 1) * sizeof(int));
    int resSize = 0, pathSize = 0;
}

// backtrack explores every subset that draws from index `start` onward.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    int* copy = malloc((size_t)(*pathSize + 1) * sizeof(int)); // +1: malloc(0) is not portable
    memcpy(copy, path, (size_t)*pathSize * sizeof(int));
    colSizes[*resSize] = *pathSize;
    res[(*resSize)++] = copy;

    // Try each remaining element as the next one to include.
    for (int i = start; i < numsSize; i++) {
        // CHOOSE: add nums[i] to the current subset.
        path[(*pathSize)++] = nums[i];

        // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
        backtrack(nums, numsSize, i + 1, path, pathSize, res, resSize, colSizes);
    }
}
#include <stdlib.h>
#include <string.h>

// Declared up front so subsets can call it before it is defined further down.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes);

// The caller owns every returned row, the row array and *returnColumnSizes.
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
    // res collects finished subsets; path is the one we're currently building.
    int total = 1 << numsSize; // there are exactly 2^n subsets, so size it up front
    int** res = malloc((size_t)total * sizeof(int*));
    int* colSizes = malloc((size_t)total * sizeof(int));
    int* path = malloc((size_t)(numsSize + 1) * sizeof(int));
    int resSize = 0, pathSize = 0;
}

// backtrack explores every subset that draws from index `start` onward.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    int* copy = malloc((size_t)(*pathSize + 1) * sizeof(int)); // +1: malloc(0) is not portable
    memcpy(copy, path, (size_t)*pathSize * sizeof(int));
    colSizes[*resSize] = *pathSize;
    res[(*resSize)++] = copy;

    // Try each remaining element as the next one to include.
    for (int i = start; i < numsSize; i++) {
        // CHOOSE: add nums[i] to the current subset.
        path[(*pathSize)++] = nums[i];

        // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
        backtrack(nums, numsSize, i + 1, path, pathSize, res, resSize, colSizes);

        // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
    }
}
#include <stdlib.h>
#include <string.h>

// Declared up front so subsets can call it before it is defined further down.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes);

// The caller owns every returned row, the row array and *returnColumnSizes.
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
    // res collects finished subsets; path is the one we're currently building.
    int total = 1 << numsSize; // there are exactly 2^n subsets, so size it up front
    int** res = malloc((size_t)total * sizeof(int*));
    int* colSizes = malloc((size_t)total * sizeof(int));
    int* path = malloc((size_t)(numsSize + 1) * sizeof(int));
    int resSize = 0, pathSize = 0;
}

// backtrack explores every subset that draws from index `start` onward.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    int* copy = malloc((size_t)(*pathSize + 1) * sizeof(int)); // +1: malloc(0) is not portable
    memcpy(copy, path, (size_t)*pathSize * sizeof(int));
    colSizes[*resSize] = *pathSize;
    res[(*resSize)++] = copy;

    // Try each remaining element as the next one to include.
    for (int i = start; i < numsSize; i++) {
        // CHOOSE: add nums[i] to the current subset.
        path[(*pathSize)++] = nums[i];

        // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
        backtrack(nums, numsSize, i + 1, path, pathSize, res, resSize, colSizes);

        // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
        (*pathSize)--;
    }
}
#include <stdlib.h>
#include <string.h>

// Declared up front so subsets can call it before it is defined further down.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes);

// The caller owns every returned row, the row array and *returnColumnSizes.
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
    // res collects finished subsets; path is the one we're currently building.
    int total = 1 << numsSize; // there are exactly 2^n subsets, so size it up front
    int** res = malloc((size_t)total * sizeof(int*));
    int* colSizes = malloc((size_t)total * sizeof(int));
    int* path = malloc((size_t)(numsSize + 1) * sizeof(int));
    int resSize = 0, pathSize = 0;

    // Kick off from the empty subset.
    backtrack(nums, numsSize, 0, path, &pathSize, res, &resSize, colSizes);
    free(path);
    *returnSize = resSize;
    *returnColumnSizes = colSizes;
    return res;
}

// backtrack explores every subset that draws from index `start` onward.
static void backtrack(int* nums, int numsSize, int start, int* path, int* pathSize,
                      int** res, int* resSize, int* colSizes) {
    // Every node of the recursion is itself a valid subset. Store a COPY,
    // because path keeps mutating under us.
    int* copy = malloc((size_t)(*pathSize + 1) * sizeof(int)); // +1: malloc(0) is not portable
    memcpy(copy, path, (size_t)*pathSize * sizeof(int));
    colSizes[*resSize] = *pathSize;
    res[(*resSize)++] = copy;

    // Try each remaining element as the next one to include.
    for (int i = start; i < numsSize; i++) {
        // CHOOSE: add nums[i] to the current subset.
        path[(*pathSize)++] = nums[i];

        // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
        backtrack(nums, numsSize, i + 1, path, pathSize, res, resSize, colSizes);

        // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
        (*pathSize)--;
    }
}
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {

    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        // res collects finished subsets; path is the one we're currently building.
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        // res collects finished subsets; path is the one we're currently building.
        vector<vector<int>> res;
        vector<int> path;
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        // res collects finished subsets; path is the one we're currently building.
        vector<vector<int>> res;
        vector<int> path;
    }

private:
    // backtrack explores every subset that draws from index `start` onward.
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        // res collects finished subsets; path is the one we're currently building.
        vector<vector<int>> res;
        vector<int> path;
    }

private:
    // backtrack explores every subset that draws from index `start` onward.
    void backtrack(vector<int>& nums, int start, vector<int>& path, vector<vector<int>>& res) {
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        // res collects finished subsets; path is the one we're currently building.
        vector<vector<int>> res;
        vector<int> path;
    }

private:
    // backtrack explores every subset that draws from index `start` onward.
    void backtrack(vector<int>& nums, int start, vector<int>& path, vector<vector<int>>& res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        // res collects finished subsets; path is the one we're currently building.
        vector<vector<int>> res;
        vector<int> path;
    }

private:
    // backtrack explores every subset that draws from index `start` onward.
    void backtrack(vector<int>& nums, int start, vector<int>& path, vector<vector<int>>& res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res.push_back(path);
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        // res collects finished subsets; path is the one we're currently building.
        vector<vector<int>> res;
        vector<int> path;
    }

private:
    // backtrack explores every subset that draws from index `start` onward.
    void backtrack(vector<int>& nums, int start, vector<int>& path, vector<vector<int>>& res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res.push_back(path);

        // Try each remaining element as the next one to include.
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        // res collects finished subsets; path is the one we're currently building.
        vector<vector<int>> res;
        vector<int> path;
    }

private:
    // backtrack explores every subset that draws from index `start` onward.
    void backtrack(vector<int>& nums, int start, vector<int>& path, vector<vector<int>>& res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res.push_back(path);

        // Try each remaining element as the next one to include.
        for (int i = start; i < (int)nums.size(); i++) {
        }
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        // res collects finished subsets; path is the one we're currently building.
        vector<vector<int>> res;
        vector<int> path;
    }

private:
    // backtrack explores every subset that draws from index `start` onward.
    void backtrack(vector<int>& nums, int start, vector<int>& path, vector<vector<int>>& res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res.push_back(path);

        // Try each remaining element as the next one to include.
        for (int i = start; i < (int)nums.size(); i++) {
            // CHOOSE: add nums[i] to the current subset.
        }
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        // res collects finished subsets; path is the one we're currently building.
        vector<vector<int>> res;
        vector<int> path;
    }

private:
    // backtrack explores every subset that draws from index `start` onward.
    void backtrack(vector<int>& nums, int start, vector<int>& path, vector<vector<int>>& res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res.push_back(path);

        // Try each remaining element as the next one to include.
        for (int i = start; i < (int)nums.size(); i++) {
            // CHOOSE: add nums[i] to the current subset.
            path.push_back(nums[i]);
        }
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        // res collects finished subsets; path is the one we're currently building.
        vector<vector<int>> res;
        vector<int> path;
    }

private:
    // backtrack explores every subset that draws from index `start` onward.
    void backtrack(vector<int>& nums, int start, vector<int>& path, vector<vector<int>>& res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res.push_back(path);

        // Try each remaining element as the next one to include.
        for (int i = start; i < (int)nums.size(); i++) {
            // CHOOSE: add nums[i] to the current subset.
            path.push_back(nums[i]);

            // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
        }
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        // res collects finished subsets; path is the one we're currently building.
        vector<vector<int>> res;
        vector<int> path;
    }

private:
    // backtrack explores every subset that draws from index `start` onward.
    void backtrack(vector<int>& nums, int start, vector<int>& path, vector<vector<int>>& res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res.push_back(path);

        // Try each remaining element as the next one to include.
        for (int i = start; i < (int)nums.size(); i++) {
            // CHOOSE: add nums[i] to the current subset.
            path.push_back(nums[i]);

            // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            backtrack(nums, i + 1, path, res);
        }
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        // res collects finished subsets; path is the one we're currently building.
        vector<vector<int>> res;
        vector<int> path;
    }

private:
    // backtrack explores every subset that draws from index `start` onward.
    void backtrack(vector<int>& nums, int start, vector<int>& path, vector<vector<int>>& res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res.push_back(path);

        // Try each remaining element as the next one to include.
        for (int i = start; i < (int)nums.size(); i++) {
            // CHOOSE: add nums[i] to the current subset.
            path.push_back(nums[i]);

            // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            backtrack(nums, i + 1, path, res);

            // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
        }
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        // res collects finished subsets; path is the one we're currently building.
        vector<vector<int>> res;
        vector<int> path;
    }

private:
    // backtrack explores every subset that draws from index `start` onward.
    void backtrack(vector<int>& nums, int start, vector<int>& path, vector<vector<int>>& res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res.push_back(path);

        // Try each remaining element as the next one to include.
        for (int i = start; i < (int)nums.size(); i++) {
            // CHOOSE: add nums[i] to the current subset.
            path.push_back(nums[i]);

            // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            backtrack(nums, i + 1, path, res);

            // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
            path.pop_back();
        }
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        // res collects finished subsets; path is the one we're currently building.
        vector<vector<int>> res;
        vector<int> path;

        backtrack(nums, 0, path, res); // kick off from the empty subset
        return res;
    }

private:
    // backtrack explores every subset that draws from index `start` onward.
    void backtrack(vector<int>& nums, int start, vector<int>& path, vector<vector<int>>& res) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res.push_back(path);

        // Try each remaining element as the next one to include.
        for (int i = start; i < (int)nums.size(); i++) {
            // CHOOSE: add nums[i] to the current subset.
            path.push_back(nums[i]);

            // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            backtrack(nums, i + 1, path, res);

            // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
            path.pop_back();
        }
    }
};

Tap the left half to go back, the right half to go forward.

Arrow keys or space to navigate · Esc to exit.