#Hello, can you please help me with optimising this code?

8 messages · Page 1 of 1 (latest)

naive oak
#
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;
  }
};```
grand coyoteBOT
#

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
green elbow
#

@naive oak why are you making your own vector?

scenic creek
#

@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)

naive oak
#

!solved

grand coyoteBOT
#

[SOLVED] Hello, can you please help me with optimising this code?

grand coyoteBOT
#

Hello, can you please help me with optimising this code?