codekofi
← All problems

Problem 35

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

1bool solution(const vector<int>& hand, int size) {
2
3 int total = hand.size();
4
5 if (size <= 0) {
6 return false;
7 }
8
9 if (total % size != 0) {
10 return false;
11 }
12
13 map<int, int> count;
14
15 for (int x : hand) {
16 count[x]++;
17 }
18
19 while (!count.empty()) {
20
21 int start = count.begin()->first;
22
23 for (int v = start; v < start + size; v++) {
24
25 auto it = count.find(v);
26
27 if (it == count.end()) {
28 return false;
29 }
30
31 it->second--;
32
33 if (it->second == 0) {
34 count.erase(it);
35 }
36 }
37 }
38
39 return true;
40}

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.