title: Find k in a k-sorted array
body: <p>I'm trying to write a binary-search-like algorithm to find k in a k-sorted array. (It needs to be O(logn))</p>
  
  <p>For example, k here is 3:
  <code>5, 6, 7, 1, 2, 3, 4</code></p>
  
  <p>Here's my try in Java</p>
  
  <pre><code>public static int findk(int[] a) {
      int low = 0;
      int high = a.length-1;
  
      while(low &lt;= high) {
          if(a[low] &lt;= a[high])
              return low;
  
          int middle = (low+high)/2;
          if(a[low] &gt; a[middle])
              high = middle-1;
          else
              low = middle+1;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>It works for some inputs but doesn't work for others. It should be really easy but I can't figure out what's wrong.</p>
  
  <p>Even a hint will be nice!</p>