Skip to content

Commit f3d9515

Browse files
authored
Create readme.md
1 parent 6860f50 commit f3d9515

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Sqrt(x)/readme.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Sqrt(x) Problem
2+
3+
## Problem Description:
4+
5+
Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.
6+
7+
You must not use any built-in exponent function or operator.
8+
9+
For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python.
10+
11+
### Example 1:
12+
- **Input:** x = 4
13+
- **Output:** 2
14+
- **Explanation:** The square root of 4 is 2, so we return 2.
15+
16+
### Example 2:
17+
- **Input:** x = 8
18+
- **Output:** 2
19+
- **Explanation:** The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned.
20+
21+
### Constraints:
22+
- 0 <= x <= 231 - 1
23+
24+
## Problem Understanding:
25+
26+
My approach to problems starts with thinking like a human rather than immediately applying coding logic.
27+
I ask myself how I would solve the problem without any programming knowledge. This helps me develop a simpler technique.
28+
Sometimes, thinking in a programming language can make things more complicated.
29+
Initially, I may come up with a complex solution, but at least I can optimize it later. However, if I dive straight into coding and things get too complicated, I might not even reach a solution.
30+
For this problem, I approached it by identifying the range in which the square root of a number (e.g., 48) falls. This method helped me narrow down the possible answers before implementing the solution in code.
31+
32+
## Time and Space Complexity
33+
34+
The time complexity of the initial solution, where you're incrementing i one by one until i * i exceeds x, is indeed O(√x). This is because you're essentially searching for the square root by trying each integer value up to the square root of x.
35+
The space complexity is O(1) since you're only using a constant amount of extra space (the variable i).
36+
However, the time complexity can be improved by using a more efficient method ... (coming soon)
37+
38+
🌟Thinking in this way is also useful for live coding practice. Because you're often asked how else it could be written.
39+
The first solution is not always the most optimized, and we cannot always know that upfront. That's why we train ourselves to think in this way.
40+

0 commit comments

Comments
 (0)