Challenge - 5 Problems
Combination Sum 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++
#include <iostream> #include <vector> using namespace std; void backtrack(int start, int k, int target, vector<int>& current, vector<vector<int>>& result) { if (target == 0 && current.size() == k) { result.push_back(current); return; } if (target < 0 || current.size() > k) return; for (int i = start; i <= 4; ++i) { current.push_back(i); backtrack(i + 1, k, target - i, current, result); current.pop_back(); } } int main() { int k = 2, target = 5; vector<vector<int>> result; vector<int> current; backtrack(1, k, target, current, result); for (auto& comb : result) { for (int num : comb) cout << num << " "; cout << "\n"; } return 0; }
Attempts:
2 left
💡 Hint
Focus on combinations of exactly 2 numbers that sum to 5 using numbers 1 to 4 without repetition.
✗ Incorrect
The code finds all combinations of length 2 from numbers 1 to 4 that sum to 5. These are [1,4] and [2,3]. Option A includes 5 alone which is invalid since k=2. Option A includes a triple which is invalid. Option A repeats [3,2] which is same as [2,3] but order matters here and duplicates are avoided.
🧠 Conceptual
intermediateNumber of combinations for sum K with length constraint
Given numbers from 1 to 5, how many unique combinations of length 3 sum to 7?
Attempts:
2 left
💡 Hint
Try to list all combinations of 3 numbers from 1 to 5 that add up to 7.
✗ Incorrect
Allowing repetition of numbers, the unique combinations (in non-decreasing order) of length exactly 3 from 1 to 5 summing to 7 are [1,1,5], [1,2,4], [1,3,3], and [2,2,3]. There are 4 such combinations.
🔧 Debug
advancedIdentify the error in combination sum code
What error does the following code produce when trying to generate all combinations of length k that sum to target?
DSA C++
#include <vector> using namespace std; void backtrack(int start, int k, int target, vector<int>& current, vector<vector<int>>& result) { if (target == 0 && current.size() == k) { result.push_back(current); return; } if (target < 0 || current.size() > k) return; for (int i = start; i <= 5; ++i) { current.push_back(i); backtrack(i, k, target - i, current, result); current.pop_back(); } } int main() { vector<vector<int>> result; vector<int> current; backtrack(1, 3, 7, current, result); return 0; }
Attempts:
2 left
💡 Hint
Check the recursive call's start parameter and how it affects the loop.
✗ Incorrect
The recursive call uses 'backtrack(i, ...)' instead of 'backtrack(i + 1, ...)'. This causes the same number to be reused infinitely, leading to infinite recursion and stack overflow.
📝 Syntax
advancedIdentify syntax error in combination sum code snippet
Which option contains the correct syntax to define a backtracking function that generates combinations of length k summing to target?
Attempts:
2 left
💡 Hint
Consider which parameters need to be passed by reference to reflect changes outside the function.
✗ Incorrect
The current combination vector and the result vector must be passed by reference to avoid copying and to allow modifications to persist. Option A correctly passes both by reference.
🚀 Application
expertFind all unique combinations summing to K with duplicates allowed
Given the numbers [2,3,6,7], which option correctly outputs all unique combinations where numbers can be chosen multiple times to sum to 7?
DSA C++
#include <iostream> #include <vector> using namespace std; void backtrack(int start, int target, vector<int>& candidates, vector<int>& current, vector<vector<int>>& result) { if (target == 0) { result.push_back(current); return; } if (target < 0) return; for (int i = start; i < candidates.size(); ++i) { current.push_back(candidates[i]); backtrack(i, target - candidates[i], candidates, current, result); current.pop_back(); } } int main() { vector<int> candidates = {2,3,6,7}; vector<vector<int>> result; vector<int> current; backtrack(0, 7, candidates, current, result); for (auto& comb : result) { for (int num : comb) cout << num << " "; cout << "\n"; } return 0; }
Attempts:
2 left
💡 Hint
Numbers can be reused, but combinations should be unique and sorted by construction.
✗ Incorrect
The code allows reuse of numbers by calling backtrack(i, ...). It generates combinations [2,2,3] and [7]. Option D has different order but same numbers, option D duplicates combinations, option D includes invalid combination with 1 which is not in candidates.
