Skip to content

Commit ee43cf0

Browse files
Merge pull request #2923 from PritomKarmokar/cpp-solution-sqrt-x
adding 0069-sqrtx.cpp
2 parents bade35b + 2309358 commit ee43cf0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

cpp/0069-sqrtx.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int mySqrt(int x) {
4+
5+
int l = 0;
6+
int r = x;
7+
8+
while(l <= r){
9+
long int m = (l + r) / 2;
10+
if(m * m == x){
11+
return m;
12+
}
13+
else if(m * m > x){
14+
r = m - 1;
15+
}
16+
else{
17+
l = m + 1;
18+
}
19+
}
20+
return r;
21+
}
22+
};

0 commit comments

Comments
 (0)