From db69d0a539d2248c61ce2849dd12f9487d998d64 Mon Sep 17 00:00:00 2001 From: hemanth8819 <119241921+hemanth8819@users.noreply.github.com> Date: Wed, 28 Jun 2023 01:19:43 +0530 Subject: [PATCH] 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 --- leetcode/src/69.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 leetcode/src/69.c diff --git a/leetcode/src/69.c b/leetcode/src/69.c new file mode 100644 index 00000000..63c0d025 --- /dev/null +++ b/leetcode/src/69.c @@ -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; +}