Challenge - 5 Problems
Combination Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of combinations summing to K
What is the output of the following C# code that generates all combinations of numbers from 1 to 4 that sum to 5?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { var results = new List<List<int>>(); GenerateCombinations(1, 4, 5, new List<int>(), results); foreach (var combo in results) { Console.WriteLine(string.Join(", ", combo)); } } static void GenerateCombinations(int start, int end, int target, List<int> current, List<List<int>> results) { if (target == 0) { results.Add(new List<int>(current)); return; } for (int i = start; i <= end; i++) { if (i > target) break; current.Add(i); GenerateCombinations(i + 1, end, target - i, current, results); current.RemoveAt(current.Count - 1); } }
Attempts:
2 left
💡 Hint
Focus on combinations of unique numbers that sum to 5 without repetition.
✗ Incorrect
The code generates combinations of unique numbers from 1 to 4 that sum to 5. The valid combinations are [1,4] and [2,3]. It does not include repeated numbers like [1,2,2] or [1,1,3].
🧠 Conceptual
intermediateUnderstanding the role of 'start' parameter
In the recursive function to generate all combinations summing to K, what is the purpose of the 'start' parameter?
Attempts:
2 left
💡 Hint
Think about how duplicates are avoided in combinations.
✗ Incorrect
The 'start' parameter ensures that each recursive call only considers numbers greater than the previous chosen number, maintaining ascending order and preventing duplicate combinations.
🔧 Debug
advancedIdentify the error in combination generation
What error will occur if the line 'current.RemoveAt(current.Count - 1);' is removed from the recursive function?
DSA C#
static void GenerateCombinations(int start, int end, int target, List<int> current, List<List<int>> results) { if (target == 0) { results.Add(new List<int>(current)); return; } for (int i = start; i <= end; i++) { if (i > target) break; current.Add(i); GenerateCombinations(i + 1, end, target - i, current, results); // current.RemoveAt(current.Count - 1); // This line is removed } }
Attempts:
2 left
💡 Hint
Consider what happens to the 'current' list after recursive calls.
✗ Incorrect
Without removing the last added number after recursion, the 'current' list keeps growing and does not backtrack properly, causing incorrect combinations to be recorded.
📝 Syntax
advancedIdentify the syntax error in combination code
Which option contains a syntax error in the following combination generation code snippet?
DSA C#
for (int i = start; i <= end; i++) { if (i > target) break current.Add(i); GenerateCombinations(i + 1, end, target - i, current, results); current.RemoveAt(current.Count - 1); }
Attempts:
2 left
💡 Hint
Check each line carefully for punctuation.
✗ Incorrect
The 'break' statement is missing a semicolon at the end, causing a syntax error.
🚀 Application
expertNumber of combinations for sum K
Given numbers from 1 to 7, how many unique combinations of numbers sum exactly to 10? The combinations must use each number at most once and order does not matter.
Attempts:
2 left
💡 Hint
Try to list or count all combinations manually or use a recursive approach.
✗ Incorrect
The unique combinations of numbers from 1 to 7 that sum to 10 are: [1,2,3,4], [1,2,7], [1,3,6], [1,4,5], [2,3,5], [3,7], [4,6]. Correct count is 7.
