title: Binary Search implementation via java
body: <p>Please see the below java source code for binary search implementation </p>
  
  <pre><code>public class Main {
      public static void main(String[] args) {
  
          int[] x = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
          int y = binarySearch(x, 11);
          System.out.println(y);
  
      }
  
      public static int binarySearch(int[] arr, int value) {
          int searchedIndex = -1;
          int first = 0;
          int last = arr.length - 1;
          int mid;
  
          while (first &lt;= last) {
              mid = (first + last) / 2;
  
              if (arr[mid] == value) {
                  searchedIndex = mid;
                  break;
              } else {
                  if (value &lt; arr[mid]) {
                      last = mid - 1;
                  } else {
                      first = mid + 1;
                  }
              }
          }
  
          return searchedIndex;
      }
  }
  </code></pre>
  
  <p><strong>int last = arr.length - 1</strong>  is -1 compulsory or not. I feel that code works fine either <strong>last = arr.length - 1</strong>. If its compulsory please explain why.</p>