codekofi
← All problems

Problem 130

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 unordered_set<int> pool(nums.begin(), nums.end());
4 int ans = 0;
5
6 for (int x : pool) {
7
8 if (pool.count(x - 1)) {
9 continue;
10 }
11
12 int len = 1;
13
14 while (pool.count(x + len)) {
15 len++;
16 }
17
18 ans = max(ans, len);
19 }
20
21 return ans;
22}

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.