Binary Search c++


  • 0
    L
            if(x <= 0)
                return 0;
            int low = 1;
            int high = x;
            while(low <= high){
                long mid = (long)low + (high - low)/2;
                long temp = mid *mid;
                if(temp == x)
                    return mid;
                else if(temp > x)
                    high = mid - 1;
                else
                    low = mid + 1;
            }
            return high;
    

Log in to reply
 

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