codekofi
← All problems

Problem 41

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 grow(const vector<int>& nums, int i, vector<int>& current,
2 vector<vector<int>>& out) {
3
4 out.push_back(current);
5
6 int n = nums.size();
7
8 for (int j = i; j < n; j++) {
9
10 if (j > i && nums[j] == nums[j - 1]) {
11 continue;
12 }
13
14 current.push_back(nums[j]);
15 grow(nums, j + 1, current, out);
16 current.pop_back();
17 }
18}
19
20vector<vector<int>> solution(vector<int> nums) {
21
22 sort(nums.begin(), nums.end());
23
24 vector<vector<int>> ans;
25 vector<int> current;
26
27 grow(nums, 0, current, ans);
28
29 return ans;
30}

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.