Why is this happening


#1

int Solution::sqrt(int A) {
int start=1;
int end = A;
int mid;
int ans =mid;
if(A==0)
return 0;
while(start<=end){
mid = (start+end)/2;
if(mid<=A/mid){ // if I changes mid<=A/mid to mid*mid<=A , compiler is showing
//TLE
start = mid+1;
ans = mid;
}
else
end = mid-1;

}
return ans;

}


#2

Thats because the testcases have cases where the int size might become small and a * a might overflow, making it negative. Therefore if we do A / mid, there is not overflow.