title: Where is the infinite loop occurring?
body: <p>This problem comes from leetcode:</p>
  
  <p>Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.</p>
  
  <p>Your algorithm's runtime complexity must be in the order of O(log n).</p>
  
  <p>If the target is not found in the array, return [-1, -1].</p>
  
  <p>Example 1:</p>
  
  <p>Input: nums = [5,7,7,8,8,10], target = 8
  Output: [3,4]</p>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  vector&lt;int&gt; searchRange(vector&lt;int&gt;&amp; nums, int target)
  {
      int left = 0;
      int right = (int)nums.size() - 1;
      std::vector&lt;int&gt; result;
      while(left &lt;= right)
      {
          auto mid = (int)(left + right) / 2;
          if(nums[mid] &lt; target)
              left = mid + 1;
          else
              right = mid;
      }
  
      int next_left = left;
      int next_right = (int) nums.size() - 1;
      while(next_left &lt; next_right)
      {
          auto mid = (int)(left + right + 1) / 2;
          if(nums[mid] &gt; target)
              next_right = mid - 1;
          else
              next_left = mid;
      }
  
      result = {next_left, next_right};
  
      return result;
  
  
  }
  
  
  int main()
  {
      std::vector&lt;int&gt; nums = {1,2,2,2,2,4};
      int target = 2;
  
      auto result = searchRange(nums, target);
      for(auto it: result)
          std::cout &lt;&lt; it &lt;&lt; " ";
  
      return 0;
  }
  </code></pre>
  
  <p>I keep getting a Time Limit Exceeded. I don't know how to fix it any help would be appreciated. </p>