codekofi
← All problems

Problem 10

Medium35 XP

1 · Worked examples

0 / 4

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 4 outputs correctly.

The accepted solution

1vector<int> solution(const vector<int>& nums) {
2 int n = nums.size();
3 vector<int> out(n, 1);
4 int running = 1;
5 for (int i = 0; i < n; i++) {
6 out[i] = running;
7 running = running * nums[i];
8 }
9 running = 1;
10 for (int i = n - 1; i >= 0; i--) {
11 out[i] = out[i] * running;
12 running = running * nums[i];
13 }
14 return out;
15}

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.