title: Modified Binary Search java
body: <p>So I'm running into a huge road block....maybe it's just because my logic is not there but I can't seem to figure this out on my own.</p>
  
  <p><strong><em>I'm trying to modify <code>BinarySearch</code> so that it will obtain two indexes.</em></strong></p>
  
  <p><strong><em>First index is the farthest left index of a number given, x, and the farthest right. If the number doesn't exist then it produces [-1,-1].</em></strong></p>
  
  <p>Anyways. I have been trying to modify the BinarySearch and can't seem to get it working. Any pointers would be greatly appreciated.</p>
  
  <pre><code>public static Pair BinarySearchDup(int[] A, int x, int low, int high){
      int mid = (low + high) / 2;
      int left = -1, right = -1;
      while(low &lt;= high){
          mid = (low + high) / 2;
          if(A[mid] == x){
              int newMid = mid;
              //check left
              if(left == -1){
                  left = mid;
                  return BinarySearchDup(A, x, low, mid - 1);
              }
              else if(right == -1){
                  right = mid;
                  return BinarySearchDup(A, x, newMid + 1, high);
              }       
              return new Pair(left, right);
          }
          else if(A[mid] &lt; x)
              return BinarySearchDup(A, x, mid + 1, high);
          else// (A[mid] &gt; x)
              return BinarySearchDup(A, x, low, mid - 1);
      }
      //if there are no matches of the number then it returns -1
      return new Pair(-1, -1);
  }
  </code></pre>