```
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
vector<int> idx(n);
iota(idx.begin(), idx.end(), 0);
sort(idx.begin(), idx.end(), [nums](int l, int r) {return nums[l] < nums[r]; });
for (int i = 0; i < n; ++i) {
int dif = target - nums[idx[i]];
if (dif < nums[idx[i]]) continue;
else {
int l = i + 1;
int r = n - 1;
int m = (l + r) / 2;
while (l <= r) {
if (nums[idx[m]] == dif) {
vector<int> res = { max(idx[i], idx[m]), min(idx[i], idx[m]) };
return res;
}
else if (nums[idx[m]] < dif) {
l = m + 1;
m = (l + r) / 2;
}
else {
r = m - 1;
m = (l + r) / 2;
}
}
}
}
}
};
```