Skip to content

Commit 85c8b8f

Browse files
Merge pull request #2928 from PritomKarmokar/c-solution-sqrtx
adding 0069-sqrtx.c
2 parents 3a02fe1 + 10537cf commit 85c8b8f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Diff for: c/0069-sqrtx.c

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
int mySqrt(int x){
2+
int l = 0;
3+
int r = x;
4+
5+
while(l <= r){
6+
long int m = (l + r) / 2;
7+
if(m * m == x){
8+
return m;
9+
}
10+
else if(m * m > x){
11+
r = m - 1;
12+
}
13+
else{
14+
l = m + 1;
15+
}
16+
}
17+
return r;
18+
}

0 commit comments

Comments
 (0)