Skip to content

Commit db69d0a

Browse files
authored
feat: add LeetCode problem 69 (#1259)
* feat: add LeetCode problem 69 Here is the code for the problem 69 of leetcode as there are many ways to do it we programmers need to find the most optimal way to solve a problem statement so i used binary-search approach inorder to solve it. All suggestions are accepted!!! * Update 69.c I have updated the solution according to the suggestions. * Update 69.c * Update 69.c
1 parent f4ee574 commit db69d0a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

leetcode/src/69.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//using the binary search method is one of the efficient ones for this problem statement.
2+
int mySqrt(int x){
3+
int start=0;
4+
int end=x;
5+
long long int ans=0;
6+
while(start <= end){
7+
long long int mid=(start+end)/2;
8+
long long int val=mid*mid;
9+
if( val == x){
10+
return mid;
11+
}
12+
//if mid is less than the square root of the number(x) store the value of mid in ans.
13+
if( val < x){
14+
ans = mid;
15+
start = mid+1;
16+
}
17+
//if mid is greater than the square root of the number(x) then ssign the value mid-1 to end.
18+
if( val > x){
19+
end = mid-1;
20+
}
21+
}
22+
return ans;
23+
}

0 commit comments

Comments
 (0)