Generate All Combinations Sum K in DSA C# - Time & Space Complexity
We want to understand how the time needed to find all combinations that add up to a target grows as the input size changes.
How does the number of possible combinations affect the work done?
Analyze the time complexity of the following code snippet.
public void FindCombinations(int[] nums, int target, int start, List<int> current, List<List<int>> result) {
if (target == 0) {
result.Add(new List<int>(current));
return;
}
for (int i = start; i < nums.Length; i++) {
if (nums[i] > target) break;
current.Add(nums[i]);
FindCombinations(nums, target - nums[i], i + 1, current, result);
current.RemoveAt(current.Count - 1);
}
}
This code finds all unique combinations of numbers from the array that sum to the target.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls exploring combinations.
- How many times: Potentially exploring many subsets, each recursive call tries next elements.
As input size grows, the number of possible combinations grows very fast.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Thousands of combinations |
| 20 | Millions of combinations |
| 30 | Billions of combinations |
Pattern observation: The work grows exponentially because we explore many subsets.
Time Complexity: O(2^n)
This means the time roughly doubles with each additional input element because we explore all possible subsets.
[X] Wrong: "The time grows linearly because we just loop through the array once."
[OK] Correct: The code uses recursion to explore many combinations, not just a single loop, so the work grows much faster than linear.
Understanding this exponential growth helps you explain why some problems need careful pruning or different approaches to be efficient.
"What if we allowed repeated use of the same element in combinations? How would the time complexity change?"
Practice
Solution
Step 1: Understand the problem goal
The problem asks to find all unique sets of numbers that add exactly to K.Step 2: Identify the correct description
All unique combinations of numbers that add up to K correctly describes finding all unique combinations summing to K.Final Answer:
All unique combinations of numbers that add up to K -> Option CQuick Check:
Combinations sum K = all sets adding to K [OK]
- Confusing sum with product
- Thinking about single numbers instead of combinations
- Choosing options about divisors or ranges
FindCombinations that takes a list of integers and a target sum?Solution
Step 1: Check parameter types and syntax
In C#, a list of integers is declared as List<int>. The method parameters must be separated by commas.Step 2: Identify correct syntax
void FindCombinations(List<int> nums, int target) { } uses List<int> and comma-separated parameters correctly. Others have syntax errors or wrong types.Final Answer:
void FindCombinations(List<int> nums, int target) { } -> Option BQuick Check:
Correct C# method syntax = void FindCombinations(List<int> nums, int target) { } [OK]
- Using array syntax int nums[] in method parameters
- Missing commas between parameters
- Using untyped List without <int>
void Generate(int start, int target, List<int> current, List result) {
if (target == 0) {
result.Add(new List<int>(current));
return;
}
for (int i = start; i <= 3; i++) {
if (i <= target) {
current.Add(i);
Generate(i + 1, target - i, current, result);
current.RemoveAt(current.Count - 1);
}
}
}
var result = new List();
Generate(1, 3, new List<int>(), result);
foreach(var combo in result) {
Console.WriteLine(string.Join(",", combo));
}Solution
Step 1: Trace recursive calls and combinations
The code tries numbers from start to 3, adding to current if <= target. It avoids repeats by increasing start each call.Step 2: Identify valid combinations summing to 3
Possible combinations are [1,2] and [3]. The code prints these two lines: "1,2" and "3".Final Answer:
1,2 3 -> Option DQuick Check:
Unique combos summing 3 = 1,2 and 3 [OK]
- Expecting repeated numbers like 1,1,1
- Confusing order of output lines
- Including reversed duplicates like 2,1
void Generate(int start, int target, List<int> current, List result) {
if (target == 0) {
result.Add(new List<int>(current));
return;
}
for (int i = start; i < 4; i++) {
if (i <= target) {
current.Add(i);
Generate(i + 1, target - i, current, result);
current.RemoveAt(current.Count - 1);
}
}
}Solution
Step 1: Analyze the for loop range
The loop runs from start to i < 4, so it stops at i=3, missing i=4.Step 2: Identify impact on combinations
Because 4 is excluded, combinations including 4 are missed. Changing to i <= 4 fixes this.Final Answer:
The loop condition should be <= 4, not < 4 -> Option AQuick Check:
Loop range excludes 4, missing combos [OK]
- Using wrong loop end condition
- Changing recursion start incorrectly
- Not copying list before adding to result
void Generate(int start, int target, List<int> current, List result) {
if (target == 0) {
result.Add(new List<int>(current));
return;
}
for (int i = start; i <= 7; i++) {
if (i <= target) {
current.Add(i);
Generate(??? , target - i, current, result);
current.RemoveAt(current.Count - 1);
}
}
}Solution
Step 1: Understand how recursion controls repeats
Using i + 1 moves start forward, preventing repeats. Using i keeps start same, allowing repeats.Step 2: Choose correct recursive call argument
To allow repeated numbers, pass i as start in recursive call.Final Answer:
Use i to allow repeats -> Option AQuick Check:
Passing i keeps current number available [OK]
- Using i + 1 disables repeats
- Using start (unchanged) may cause infinite loops
- Restarting from 1 causes duplicates and inefficiency
Ready to prove it?
Pass the test and this pattern moves unknown
