mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-23 13:59:39 +03:00
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
This commit is contained in:
parent
f4ee5743af
commit
db69d0a539
23
leetcode/src/69.c
Normal file
23
leetcode/src/69.c
Normal file
@ -0,0 +1,23 @@
|
||||
//using the binary search method is one of the efficient ones for this problem statement.
|
||||
int mySqrt(int x){
|
||||
int start=0;
|
||||
int end=x;
|
||||
long long int ans=0;
|
||||
while(start <= end){
|
||||
long long int mid=(start+end)/2;
|
||||
long long int val=mid*mid;
|
||||
if( val == x){
|
||||
return mid;
|
||||
}
|
||||
//if mid is less than the square root of the number(x) store the value of mid in ans.
|
||||
if( val < x){
|
||||
ans = mid;
|
||||
start = mid+1;
|
||||
}
|
||||
//if mid is greater than the square root of the number(x) then ssign the value mid-1 to end.
|
||||
if( val > x){
|
||||
end = mid-1;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
Loading…
Reference in New Issue
Block a user