codekofi
← All problems

Problem 4

Easy

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

1vector<int> solution(const vector<int>& nums, int target) {
2
3 int n = nums.size();
4 unordered_map<int, int> seen;
5
6 for (int i = 0; i < n; i++) {
7
8 int need = target - nums[i];
9
10 if (seen.count(need)) {
11 return {seen[need], i};
12 }
13
14 seen[nums[i]] = i;
15 }
16
17 return {};
18}

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.