MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / twoSum

Function twoSum

CPP/arrays/Two sum 2/twoSum.cpp:4–21  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2#include<bits/stdc++.h>
3using namespace std;
4vector<int> twoSum(vector<int>& numbers, int target) {
5 vector<int> res;
6 int n = numbers.size();
7 int low = 0,high = n - 1;
8 while(low<high){
9 int sum = numbers[low] + numbers[high];
10 if(sum>target)
11 high -= 1;
12 else if(sum<target)
13 low += 1;
14 else{
15 res.push_back(low+1);
16 res.push_back(high+1);
17 break;
18 }
19 }
20 return res;
21}
22int main(){
23
24 vector<int> numbers({2,3,4});

Callers 1

mainFunction · 0.85

Calls 2

push_backMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected