class Solution
{
public:
vector<int> twoSum(vector<int> &numbers, int target)
{
vector<int> v;
for (int i = 0; i < size(numbers); i++)
{
int l = 0, r = size(numbers) - 1, m;
while (l <= r)
{
m = (l + r) / 2;
if ((numbers[i] + numbers[m] == target) and (i != m))
{
v.push_back(i + 1);
v.push_back(m + 1);
return v;
}
if (numbers[m] + numbers[i] < target)
l = m + 1;
else if (numbers[m] + numbers[i] > target)
r = m - 1;
}
}
return v;
}
};```
#Hello, can you please help me with optimising this code?
8 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.
@naive oak why are you making your own vector?
@naive oak as I mentioned in the other server, you are solving in O(N log N) a problem that is solvable in O(N)
!solved
[SOLVED] Hello, can you please help me with optimising this code?
Hello, can you please help me with optimising this code?