Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add LeetCode problem 69 #1259

Merged
merged 7 commits into from
Jun 27, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions leetcode/src/69.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//using binary search method is one of the efficient one 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;
Asp-Codes marked this conversation as resolved.
Show resolved Hide resolved
if(mid*mid == x){
return mid;
}
//if mid is less than the square root of the number(x) store the value of mid in ans.
if( mid*mid < x){
Asp-Codes marked this conversation as resolved.
Show resolved Hide resolved
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( mid*mid > x){
Asp-Codes marked this conversation as resolved.
Show resolved Hide resolved
end = mid-1;
}
}
return ans;

}