code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

Squareroot

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

class SquareRoot {

  static int func(int num) {
    int start = 0;
    int end = num;
    int ans = -1;
    while (start < end) {
      int mid = (start + end) / 2;
      int square = mid * mid;
      if (square == num) {
        return mid;
      } else {
        if (square < num) {
          start = mid + 1;
          ans = mid;
        } else {
          end = end - 1;
        }
      }
    }
    return ans;
  }

  public static void main(String[] args) {
    int result = func(181);
    System.out.println(result);
  }
}

Created 2021-12-22T07:28:38+00:00 · Edit