beats 100.00 % .


  • 0
    R
    int binary_search(long long int x, long long int first , long long int last) {
      long long int minn = 0;
      while(last - first >=0) {
        long long int middle = ((long long int ) (first + last))/2;
        if(((long long int )(middle+1)*(long long int)(middle+1) > x) && ( (long long int )middle*(long long int )middle <= x)) {
          minn = middle;
          break;
        }
        else if((long long int )middle*(long long int )middle < x ){
          first = middle + 1;
        }
        else if((long long int )middle*(long long int )middle > x) last = middle - 1;
      }
      return minn;
    }
    int mySqrt(int x) {
    
      return binary_search(x,1,x);
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.