codekofi
← All problems

Problem 12

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

1int solution(const vector<int>& nums) {
2 int n = nums.size();
3 int running = nums[0];
4 int best = nums[0];
5 for (int i = 1; i < n; i++) {
6 if (running < 0) running = 0;
7 running = running + nums[i];
8 if (running > best) best = running;
9 }
10 return best;
11}

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.