public int sqrt(int x) {

    long i = 0;
    // why increased by one ???
    // think if x is 1, x/2 = 0, so need to add one.
    long j = x / 2 + 1;

    while (i <= j) {
      long mid = (i + j) / 2;
      long midSq = mid * mid;
      if (midSq == x) {
        return (int) mid;
      } else if (x < midSq) {
        j = mid - 1;
      } else {
        i = mid + 1;
      }
    }

    // why j ???
    System.out.println("i = " + i + " j = " + j);
    return (int) j;
  }