codekofi
← All problems

Problem 58

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

1int solution(const vector<int>& nums) {
2
3 if (nums.empty()) {
4 return 0;
5 }
6
7 int n = nums.size();
8
9 int ans = nums[0];
10 int high = nums[0];
11 int low = nums[0];
12
13 for (int i = 1; i < n; i++) {
14
15 int x = nums[i];
16 int a = high * x;
17 int b = low * x;
18
19 high = max(x, max(a, b));
20 low = min(x, min(a, b));
21
22 ans = max(ans, high);
23 }
24
25 return ans;
26}

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.