Skip to content

Commit 19b5546

Browse files
authored
Create 167. Two Sum II - Input Array Is Sorted
1 parent 93e9c1e commit 19b5546

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
vector<int> twoSum(vector<int>& nums, int target)
4+
{
5+
int n=nums.size();
6+
vector< pair<int, int>>p;
7+
for(int i=0; i<n; i++)
8+
{
9+
p.push_back(make_pair(nums[i],i+1));
10+
}
11+
sort(p.begin(), p.end());
12+
13+
int i=0, j=n-1;
14+
while(i<j)
15+
{
16+
if((p[i].first + p[j].first)==target)
17+
return {p[i].second, p[j].second};
18+
else if((p[i].first + p[j].first)<target)
19+
i++;
20+
else
21+
j--;
22+
}
23+
return {};
24+
}
25+
};

0 commit comments

Comments
 (0)