codekofi
← All problems

Problem 12

Medium

1 · Worked examples

0 / 4

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

The accepted solution

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