codekofi
← All problems

Problem 22

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(vector<int>& nums, int i, vector<vector<int>>& out) {
2 int n = nums.size();
3 if (i == n) {
4 out.push_back(nums);
5 return;
6 }
7
8 for (int j = i; j < n; j++) {
9 swap(nums[i], nums[j]);
10 build(nums, i + 1, out);
11 swap(nums[i], nums[j]);
12 }
13}
14
15vector<vector<int>> solution(vector<int> nums) {
16 vector<vector<int>> out;
17 build(nums, 0, out);
18 return out;
19}

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.