-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
August-13.cpp
48 lines (40 loc) · 954 Bytes
/
August-13.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// Function to find square root
// x: element to find square root
class Solution {
public:
long long int floorSqrt(long long int n) {
if (n == 0 || n == 1)
return n;
long long int start = 1, end = n, ans = 0;
while (start <= end) {
long long int mid = start + (end - start) / 2;
long long int square = mid * mid;
if (square == n)
return mid;
if (square < n) {
ans = mid;
start = mid + 1;
} else {
end = mid - 1;
}
}
return ans;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
Solution obj;
cout << obj.floorSqrt(n) << endl;
}
return 0;
}
// } Driver Code Ends