Why is this happening


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

return ans;



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.