Combination Sum
To solve this problem, you can use a recursive approach called backtracking. The idea is to explore all possible combinations of the given candidates that sum up to the target, and store the valid combinations.
Here’s a stepbystep guide:
Initialize a Result List: This will hold all the valid combinations.
Define a Recursive Function: This function will take the current combination, the remaining target, and the starting index of the candidates as parameters.
Base Case: If the remaining target is zero, add the current combination to the result list.
Recursive Case: Iterate through the candidates starting from the given index. For each candidate:
 Add it to the current combination.
 Make a recursive call with the updated combination, reduced target by the candidate’s value, and the current index.
 Remove the last added candidate from the current combination to backtrack.
Return the Result List: Call the recursive function initially with an empty combination, the given target, and starting index 0. Return the result list.
Here’s the code:


This code returns all the unique combinations of candidates where the chosen numbers sum to the target. The time complexity of the solution is exponential, specifically (O(2^N \cdot k)), where (N) is the number of candidates and (k) is the maximum number of repeated elements for a candidate. The space complexity is (O(k)) for storing the current combination.
Identifying Problem Isomorphism
“Combination Sum” has an approximate isomorphism “Combination Sum II”
“Combination Sum” requires finding all unique combinations in a collection of candidate numbers where the sum of the numbers is equal to a target number, and the same number can be chosen from the candidate set an unlimited number of times.
“Combination Sum II” is a variant where the same number in the candidate set can only be used once in each unique combination that sums to the target.
The reasoning is that both problems require exploring all combinations of numbers that sum to a specific target. The difference lies in the constraints  while “Combination Sum” allows unlimited usage of each number, “Combination Sum II” limits usage to once per number. Although it is not an exact match, the two problems share significant similarity in their problem structure, especially in terms of the backtracking strategy used to explore all possible combinations. Thus, understanding the solution to “Combination Sum” could provide insights into solving “Combination Sum II”, and vice versa.
We need to make sum equal to the target. The numbers can be any numbers in the candidates array. Multiple times an element can be used.
Classify the problem type.
 Backtracking Algorithm
 Knapsack Problem Fractional or 01 ? 01 Knapsack Problem Bounded or Unbounded ? Unbounded
01 Unbounded Knapsack Problem
Unique Combinations [2,2,3] Membership is important Order does not matter
Recursive Approach Base Case We need to check if the current sum is equal to the target sum When we reach the target then we need to add the collected numbers to the output array (sum all the numbers in the combination, when it reaches the target)
What happens if we exceed the target. We will undo the decision we made earlier and try something else
Find all the combinations.
Incrementally build combinations by recursive calls.
Discard infeasible candidates if it does not meet the target.
How many recursive calls should I make in the code?
Depth First Traversal
Unit of Work What is the smallest slice of work that needs to be done in each recursive call? We need to get the number from candidates for a given index and add it to the partial solution (combination)
When do we do this unit of work? Before or after the recursive call? Before the recursive call.
How do we keep track of the remaining sum? We pass the diff to the recursive call as the parameter remaining parameter is used in the backtrack method.
How do we prune the tree? Check if the remaining parameter has become negative? Return; (stop recursive calls)
There will be an index that is going to be a parameter in the backtracking method.
[]  Initial state [2]  Unit of work [2,2] [2,2,3]


“Combination Sum” is a classic problem that involves recursive algorithms and backtracking. Here are 10 problems of a lesser complexity to prepare for it:
78. Subsets: This problem requires generating all possible subsets from a list of unique integers. This will help you practice working with recursive calls and manipulating lists.
39. Combination Sum: This is the problem in question, but it can help to first solve it without the constraint of a target sum.
216. Combination Sum III: This problem is a slight variation on the original, with additional constraints that can make it easier to solve.
22. Generate Parentheses: This problem requires generating all valid combinations of parentheses pairs, which involves similar recursive and backtracking concepts.
17. Letter Combinations of a Phone Number: This problem asks you to generate all possible letter combinations that the number could represent.
46. Permutations: This problem requires generating all permutations of a list, which also involves recursive calls and backtracking.
79. Word Search: This problem requires finding a word within a grid, which will help you practice backtracking and dealing with twodimensional structures.
131. Palindrome Partitioning: This problem asks for all possible ways to partition a string such that every substring of the partition is a palindrome.
200. Number of Islands: This problem helps you understand the concept of DFS and when to use it.
93. Restore IP Addresses: This problem requires generating all possible valid IP addresses from a string, which will involve similar recursive and backtracking techniques.
These problems are selected to progressively introduce you to the concept of backtracking and recursion which are heavily used in problems like “Combination Sum”.
Clarification Questions
Problem Analysis and Key Insights
Identifying Problem Isomorphism
The “Combination Sum” problem (LeetCode problem #39) can be mapped to the Unbounded Knapsack problem. Here’s how:
Items in the knapsack problem: These are analogous to the numbers in the given array in the “Combination Sum” problem.
Weights of items in the knapsack problem: These correspond to the values of numbers in the “Combination Sum” problem. Each number “weighs” as much as its own value.
Maximum weight capacity of the knapsack: This is analogous to the target number in the “Combination Sum” problem. The total “weight” of the numbers in a combination should not exceed the target.
Total value of items in the knapsack: In the “Combination Sum” problem, instead of trying to maximize the total value, we are trying to find combinations that add up to a specific target value.
Each item can be used multiple times in the knapsack problem: This is directly analogous to how each number in the given array can be used multiple times in a combination in the “Combination Sum” problem.
In terms of problemsolving strategy, we could use dynamic programming for both the Unbounded Knapsack problem and the “Combination Sum” problem. For “Combination Sum”, we would maintain a DP array where DP[i] represents the combinations that make up i. For each number in the array, we’d update the DP array from that number to target, adding the DP value of the current target minus the current number.
Please note that “Combination Sum” maps more directly to the “Coin Change” problem (LeetCode problem #322), which is a simplified version of the Unbounded Knapsack problem where the goal is to reach an exact target, not to maximize value subject to a weight constraint.
The problem on LeetCode that most closely resembles the classic 0/1 Knapsack problem is “0/1 Matrix” (LeetCode problem #474).
In this problem, you’re given a binary string array and two integers m and n, and you’re asked to find the maximum number of strings from the array that you can form with exactly m 0s and n 1s. This problem maps to the 0/1 Knapsack problem because each string in the array is an item with a weight (number of 0s and 1s in the string), and you’re trying to maximize the total number of strings subject to the constraints of m 0s and n 1s (knapsack capacity).
Another LeetCode problem that is similar to the 0/1 Knapsack problem is “Partition Equal Subset Sum” (LeetCode problem #416). In this problem, you’re given a nonempty array of positive integers, and you’re asked to determine if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. This is essentially a 0/1 Knapsack problem where the capacity of the knapsack is half the total sum of the array elements.
Please note that neither of these problems is a perfect match to the classic 0/1 Knapsack problem, but they are the closest problems available on LeetCode.
How did you identify that this problem is a variation of problem?


Problem Classification
Based on the problem domain, the problem can be classified as follows:
Combinatorics: The problem is about finding all valid combinations of k numbers, which falls under the realm of combinatorics.
Constraint Satisfaction: This problem has multiple constraints that need to be satisfied in finding the valid combinations (numbers should sum up to n, each number is used at most once, etc.). Thus, it can be viewed as a Constraint Satisfaction Problem (CSP).
Subset Generation: The problem requires generating subsets of a given set (numbers 1 through 9 in this case), but with specific constraints, hence it involves subset generation.
Backtracking: Although this term typically relates to a solution strategy, from a problem domain perspective, problems that involve exploring all possible combinations or permutations to meet certain criteria often require a backtrackinglike approach. This involves exploring a possible solution, and if a dead end is reached, ‘backtracking’ to a previous step and trying a different path.
Number Theory: Given that the problem involves working with numbers and their sums, it can also fall under the category of Number Theory.
Clarification Questions
What are the clarification questions we can ask about this problem?
Identifying Problem Isomorphism
Can you help me with finding the isomorphism for this problem?
Which problem does this problem map to the corresponding isomorphic problem on Leetcode ?
Language Agnostic Coding Drills
Here’s how you can break down this code into smaller units for learning:
Understanding Basic Programming Concepts: Before starting, one should be familiar with basic programming concepts like variables, data types (e.g., integers, lists), loops, and conditional statements.
Understanding Functions: Understanding what functions are, how to define them, how they work, and how they can call themselves (recursion).
Understanding Lists: Familiarize with lists, how to manipulate them, append elements, and use them in function arguments.
Understanding Sorting: Learn how to use sort() function to sort a list in increasing order.
Understanding Recursion: Understanding the concept of recursion, which involves a function calling itself. In this case, the
dfs
function is a recursive function.Understanding Backtracking: Understanding the concept of backtracking, which is a fundamental concept used in the algorithm to explore all possibilities until the solution is found or all possibilities have been exhausted. In this code, backtracking occurs when the target becomes less than 0.
Understanding DepthFirst Search (DFS): DFS is a traversing or searching tree or graph data structures algorithm. The algorithm starts at the root (selecting some arbitrary node as the root in the case of a graph) and explores as far as possible along each branch before backtracking.
Working with Multiple Function Parameters: This code demonstrates a function (
dfs
) with multiple parameters. Understanding how to define a function with multiple parameters and how to pass arguments to such a function is a key concept.List Comprehensions: Understanding how to use list comprehensions to create new lists. Here it is used in the line
path+[nums[i]]
.ProblemSolving with Recursion and Backtracking: Finally, understanding how all these concepts work together to solve a complex problem like finding combinations that sum to a target value. This involves understanding the problem, designing a solution, and then implementing that solution using the concepts described above.
Targeted Drills in Python
Understanding Basic Programming Concepts Here is a simple drill to understand variables, data types, and conditional statements.
1 2 3 4 5 6 7 8 9
# Assigning values to variables a = 10 b = 20 # Conditional statement if a > b: print("a is greater than b") else: print("b is greater than a")
Understanding Functions Write a function that takes two numbers and returns their sum.
1 2 3 4
def add_numbers(a, b): return a + b print(add_numbers(5, 7))
Understanding Lists Create a list, add elements to it, and print each element from the list.
1 2 3 4 5
myList = [1, 2, 3] myList.append(4) for i in myList: print(i)
Understanding Sorting Create a list with unsorted elements and sort it using the sort function.
1 2 3 4
myList = [3, 1, 4, 1, 5, 9] myList.sort() print(myList)
Understanding Recursion Write a function to calculate factorial of a number using recursion.
1 2 3 4 5 6 7
def factorial(n): if n == 0: return 1 else: return n * factorial(n1) print(factorial(5))
Understanding Backtracking Write a function to solve the NQueens problem using backtracking.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
def solveNQueens(n): def can_place(pos, ocuppied_positions): for i in range(len(ocuppied_positions)): if ocuppied_positions[i] == pos or \ ocuppied_positions[i]  i == pos  len(ocuppied_positions) or \ ocuppied_positions[i] + i == pos + len(ocuppied_positions): return False return True def place_queen(n, index, ocuppied_positions): if index == n: return [ocuppied_positions] else: solution = [] for pos in range(n): if can_place(pos, ocuppied_positions): solution += place_queen(n, index + 1, ocuppied_positions + [pos]) return solution solutions = place_queen(n, 0, []) return [["." * i + "Q" + "." * (n  i  1) for i in sol] for sol in solutions] for solution in solveNQueens(4): for line in solution: print(line) print("\n")
Understanding DepthFirst Search (DFS) Implement DepthFirst Search in a graph.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
graph = { 'A' : ['B','C'], 'B' : ['D', 'E'], 'C' : ['F'], 'D' : [], 'E' : ['F'], 'F' : [] } visited = set() def dfs(visited, graph, node): if node not in visited: print (node) visited.add(node) for neighbour in graph[node]: dfs(visited, graph, neighbour) dfs(visited, graph, 'A')
Working with Multiple Function Parameters Write a function that calculates the volume of a cylinder given
its height and radius.


List Comprehensions Write a list comprehension that creates a new list containing the squares of all numbers from 0 to 9.
1 2
squares = [x**2 for x in range(10)] print(squares)
ProblemSolving with Recursion and Backtracking Implement a function to solve the problem of generating all permutations of a string using recursion and backtracking.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
def permute(s, answer): if (len(s) == 0): print(answer, end = " ") return for i in range(len(s)): ch = s[i] left_substr = s[0:i] right_substr = s[i + 1:] rest = left_substr + right_substr permute(rest, answer + ch) answer = "" s = input("Enter the string : ") print("All possible strings are : ") permute(s, answer)