This problem follows the Include/Exclude pattern, commonly found in the Backtracking category. Recognizing this pattern is key to solving it efficiently in an interview setting.
At each element, either include or exclude it. Recurse on remaining elements.
The 'append then pop' pattern is the core of backtracking — you explore a branch, undo the choice, then try the next option.
result = []
def backtrack(start, current):
result.append(current[:])
for i in range(start, len(nums)):
current.append(nums[i])
backtrack(i + 1, current)
current.pop()
backtrack(0, [])
return resultPractice Subsets and similar Backtracking problems with flashcards. Build pattern recognition through active recall.
Practice this problem