codekofi
← All problems

Problem 22

Medium

1 · Worked examples

0 / 3

Type an input and write what you think is the output . Each problem is converted to Web Assembly, so any possible input will show the corresponding output. 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
3 int n = nums.size();
4
5 if (i == n) {
6 out.push_back(nums);
7 return;
8 }
9
10 for (int j = i; j < n; j++) {
11 swap(nums[i], nums[j]);
12 build(nums, i + 1, out);
13 swap(nums[i], nums[j]);
14 }
15}
16
17vector<vector<int>> solution(vector<int> nums) {
18
19 vector<vector<int>> ans;
20
21 build(nums, 0, ans);
22
23 return ans;
24}

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.