Square root problem

class Solution {
public:
int mySqrt(int x) {
int s=0;
int e=x;
int ans=-1;
while(s<=e){
int mid=(s+e)/2;
if(midmid==x){
return mid;
}
if(mid
mid<x){
ans=mid;
s=mid+1;
}
else{
e=mid-1;
}
}
return ans;

}

};

this is my code and i am getting error on submit it on leetcode
Line 9: Char 19: runtime error: signed integer overflow: 1073697799 * 1073697799 cannot be represented in type ‘int’ (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:18:19

Hey @sahudilip138
Overflow ho rha h

class Solution {
public:
int mySqrt(int x) {
    int s=0;
    int e=x;
    int ans=-1;
    while(s<=e){
        int mid=(s+e)/2;
        if(1ll*mid*mid==x){ //multiplied 1 long long 
            return mid;
        }
        if(1ll*mid*mid<x){ //multiplied 1 long long 
            ans=mid;
            s=mid+1;
        }
        else{
            e=mid-1;
        }
    }
    return ans;

}
};

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.