Floor and Ceil in Sorted Array in DSA Python - Time & Space Complexity
We want to know how fast we can find the floor or ceil of a number in a sorted array.
How does the time needed change as the array gets bigger?
Analyze the time complexity of the following code snippet.
def floor_ceil(arr, x):
low, high = 0, len(arr) - 1
floor_val, ceil_val = -1, -1
while low <= high:
mid = (low + high) // 2
if arr[mid] == x:
return arr[mid], arr[mid]
elif arr[mid] < x:
floor_val = arr[mid]
low = mid + 1
else:
ceil_val = arr[mid]
high = mid - 1
return floor_val, ceil_val
This code finds the floor and ceil of x in a sorted array using binary search.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that halves the search space each time.
- How many times: At most, it runs about log base 2 of n times, where n is the array size.
Each step cuts the array size roughly in half, so the number of steps grows slowly as the array grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 steps |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: Doubling the input size adds only one extra step.
Time Complexity: O(log n)
This means the time to find floor and ceil grows very slowly even if the array becomes very large.
[X] Wrong: "Since we check elements one by one, the time is O(n)."
[OK] Correct: The code does not check every element; it smartly jumps around by halving the search space each time, so it runs much faster.
Knowing how to quickly find floor and ceil using binary search shows you understand efficient searching, a key skill in many coding problems.
"What if the array was not sorted? How would the time complexity change?"
Practice
floor of a number mean in a sorted array context?Solution
Step 1: Understand floor definition in sorted array
The floor of a number is the greatest element in the array that is less than or equal to the given number.Step 2: Compare options with definition
The greatest element less than or equal to the number matches the definition exactly, others describe different concepts.Final Answer:
The greatest element less than or equal to the number -> Option AQuick Check:
Floor = greatest ≤ number [OK]
- Confusing floor with ceil
- Choosing smallest element greater than number
- Thinking floor is average of smaller elements
x in a sorted list arr using binary search?Solution
Step 1: Recall bisect_right usage for floor
bisect_right(arr, x) returns insertion point to the right of existing entries equal to x.Step 2: Find floor index using bisect_right
The floor is at index bisect_right(arr, x) - 1 if index is valid.Final Answer:
Use bisect_right and check index-1 element -> Option BQuick Check:
Floor index = bisect_right - 1 [OK]
- Using bisect_left for floor without adjusting index
- Checking index instead of index-1
- Confusing bisect_left and bisect_right roles
arr = [1, 2, 4, 6, 8, 10], write a function to find the floor and ceil of x = 5. What should the function return?Solution
Step 1: Identify floor of 5 in arr
Floor is greatest element ≤ 5, which is 4.Step 2: Identify ceil of 5 in arr
Ceil is smallest element ≥ 5, which is 6.Final Answer:
[4, 6] -> Option DQuick Check:
Floor = 4, Ceil = 6 [OK]
- Returning x if not in array
- Confusing floor and ceil values
- Choosing elements not closest to x
import bisect arr = [1, 3, 5, 7, 9] x = 6 floor_index = bisect.bisect_right(arr, x) - 1 ceil_index = bisect.bisect_left(arr, x) print(arr[floor_index], arr[ceil_index])
Solution
Step 1: Calculate floor_index using bisect_right
bisect_right(arr, 6) returns 3 (position after 5), so floor_index = 3 - 1 = 2, arr[2] = 5.Step 2: Calculate ceil_index using bisect_left
bisect_left(arr, 6) returns 3 (position of 7), so ceil_index = 3, arr[3] = 7.Final Answer:
5 7 -> Option AQuick Check:
Floor = 5, Ceil = 7 [OK]
- Using bisect_left for floor index
- Mixing floor and ceil indices
- Off-by-one errors in indexing
x in a sorted list arr. What is the error?import bisect arr = [2, 4, 6, 8] x = 1 index = bisect.bisect_right(arr, x) - 1 print(arr[index])
Solution
Step 1: Calculate index for x=1
bisect_right(arr, 1) returns 0, so index = 0 - 1 = -1.Step 2: Issue with negative index
arr[-1] accesses the last element 8 due to Python's negative indexing, but since 1 < arr[0], no element ≤1 exists, so 8 is the wrong floor value.Final Answer:
Wrong floor value printed -> Option CQuick Check:
index < 0 means no floor [OK]
- Not handling case when x < smallest element
- Assuming floor always exists
- Ignoring negative indices in Python lists
Ready to prove it?
Pass the test and this pattern moves unknown
