```
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.

```
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.

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.

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

int ans = 0 ;

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

even less .