Simple is better


#1
public int sqrt(int a) {
    if (a <= 1) {
        return a;
    }
    
    long r = 1;
    while (r*r <= a) {
        r++;
    }
    
    if (r * r != a) {
        r--;
    }
    
    return (int)r;
}

That’s all. No need for Binary Search.


#2

Yes, but your algorithm is O(sqrt(A)), and O(sqrt(A)) > O(log(A)).


#3

It doesn’t matter if it passes the constraints of the problem, which it does in this case. For very large input sets, the logarithmic approach would be better.


#4

time limit exeed for this input 282436067


#5

Its better to use binary search and rather than checking (mid*mid == A) check mid = A/mid to overcome overflow issues;


#6

int ans = 0 ;

for(long long  int i = 1 ; i*i<=A ; i++){
     ans = i ;
}
return ans ;

even less .