codekofi
← All problems

Problem 113

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

1vector<int> solution(const vector<int>& nums, int target) {
2
3 int n = nums.size();
4 int i = 0;
5 int j = n - 1;
6
7 while (i < j) {
8
9 int sum = nums[i] + nums[j];
10
11 if (sum == target) {
12 return {i + 1, j + 1};
13 }
14
15 if (sum < target) {
16 i++;
17 } else {
18 j--;
19 }
20 }
21
22 return {};
23}

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.