diff --git a/LeetCode/Sqrt(x)/C++/Screenshot.png b/LeetCode/Sqrt(x)/C++/Screenshot.png new file mode 100644 index 0000000..650fe0f Binary files /dev/null and b/LeetCode/Sqrt(x)/C++/Screenshot.png differ diff --git a/LeetCode/Sqrt(x)/C++/main.cpp b/LeetCode/Sqrt(x)/C++/main.cpp new file mode 100644 index 0000000..e83c304 --- /dev/null +++ b/LeetCode/Sqrt(x)/C++/main.cpp @@ -0,0 +1,52 @@ +#include +using namespace std; + +class Solution { +public: + + long long int binarySearch(int n){ + int s=0; + int e=n; + long long int mid = s+(e-s)/2; + long long int ans = -1; + + while(s<=e){ + long long int square = mid*mid; + if(square == n){ + return mid; + } + if(square < n){ + ans = mid; + s=mid+1; + } + else{ + e=mid-1; + } + mid = s+(e-s)/2; + } + return ans; + } + int mySqrt(int x) { + return(binarySearch(x)); + } +}; + +int main(){ + + Solution solution; + + int num = 8; + + long long result = solution.mySqrt(num); + + if (result) + { + cout<