codekofi
← All problems

Problem 104

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>& nums) {
2
3 int total = 0;
4
5 for (int x : nums) {
6 total += x;
7 }
8
9 if (total % 2 != 0) {
10 return false;
11 }
12
13 int half = total / 2;
14
15 vector<bool> reachable(half + 1, false);
16 reachable[0] = true;
17
18 for (int x : nums) {
19 for (int sum = half; sum >= x; sum--) {
20
21 if (reachable[sum - x]) {
22 reachable[sum] = true;
23 }
24 }
25 }
26
27 return reachable[half];
28}

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.