Skip to content

Commit 9bffa24

Browse files
sqrt
1 parent c7bf2d0 commit 9bffa24

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

Searching/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Easy
44
|S.no.|Question|Solution|
55
|---|------------|------------|
6-
|1|[Square Root](https://leetcode.com/problems/sqrtx/)|Pending|
6+
|1|[Square Root](https://leetcode.com/problems/sqrtx/)|[Solution](sqrt.cpp)|
77
|2| [First Bad Version](https://leetcode.com/problems/first-bad-version/)|Pending|
88
|3| [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/)|Pending|
99
|4| [Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/)|Pending|

Searching/sqrt.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int mySqrt(int x)
5+
{
6+
int low = 0, high = x;
7+
long mid;
8+
while (low <= high)
9+
{
10+
mid = low + (high - low) / 2;
11+
if (mid * mid <= x)
12+
low = mid + 1;
13+
else
14+
high = mid - 1;
15+
}
16+
return low - 1;
17+
}
18+
19+
int main()
20+
{
21+
int t;
22+
cin >> t;
23+
while (t--)
24+
{
25+
int n;
26+
cin >> n;
27+
cout<<mySqrt(n)<<endl;
28+
}
29+
return 0;
30+
}

0 commit comments

Comments
 (0)