codekofi
← All problems

Problem 17

Medium40 XP

1 · Worked examples

0 / 3

Give it an input and say what you think comes back. It compiles and runs for real, so a wrong guess still shows you the answer — probe as much as you like. Only correct predictions count.

solution()
returns

2 · Which problem is it?

Locked until you have predicted 3 outputs correctly.

The accepted solution

1void build(const vector<int>& nums, int i, vector<int>& current,
2 vector<vector<int>>& out) {
3 int n = nums.size();
4 if (i == n) {
5 out.push_back(current);
6 return;
7 }
8
9 current.push_back(nums[i]);
10 build(nums, i + 1, current, out);
11 current.pop_back();
12
13 build(nums, i + 1, current, out);
14}
15
16vector<vector<int>> solution(const vector<int>& nums) {
17 vector<vector<int>> out;
18 vector<int> current;
19 build(nums, 0, current, out);
20 return out;
21}

Names have been stripped. The signature is the only clue you get for free. Compiled as C++20 with the standard headers and using namespace std; already in scope.