Skip to content

Commit 1c146ce

Browse files
author
2025
committed
Update
1 parent 1319ab0 commit 1c146ce

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

  • src/main/java/org/example/g0001_0100/s0035_search_insert_position
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
**Time Complexity (Big O Time):**
2+
3+
1. The program performs a binary search on a sorted array of length n. In each step of the binary search, it reduces the search range by half.
4+
5+
2. The while loop runs until the `lo` pointer is less than or equal to the `hi` pointer, and in each iteration, it reduces the search range by half.
6+
7+
3. Therefore, the number of iterations in the binary search is proportional to log2(n), where n is the number of elements in the input array. This gives us the time complexity of O(log n).
8+
9+
**Space Complexity (Big O Space):**
10+
11+
The space complexity of the program is O(1), which means it uses a constant amount of additional space regardless of the size of the input array `nums`. The program only uses a few integer variables (`lo`, `hi`, `mid`) and does not use any additional data structures or memory that scales with the input size.
12+
13+
In summary, the time complexity of the provided program is O(log n), and the space complexity is O(1), where n is the number of elements in the input array `nums`.

0 commit comments

Comments
 (0)