Floor and Ceil in Sorted Array in DSA Go - 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.
func floorCeil(arr []int, x int) (int, int) {
low, high := 0, len(arr)-1
floor, ceil := -1, -1
for low <= high {
mid := low + (high-low)/2
if arr[mid] == x {
return arr[mid], arr[mid]
} else if arr[mid] < x {
floor = arr[mid]
low = mid + 1
} else {
ceil = arr[mid]
high = mid - 1
}
}
return floor, ceil
}
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 loop that halves the search space each time (binary search).
- How many times: The loop runs about log base 2 of n times, where n is the array size.
Each step cuts the array 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 more step.
Time Complexity: O(log n)
This means the time to find floor and ceil grows slowly, even if the array gets very large.
[X] Wrong: "Since we check elements one by one, it takes O(n) time."
[OK] Correct: The code uses binary search, which skips many elements each step, so it does not check all elements.
Knowing how to use binary search to quickly find floor and ceil is a useful skill that shows you understand efficient searching.
"What if the array was not sorted? How would the time complexity change?"
Practice
floor of a number mean in a sorted array?Solution
Step 1: Understand the floor definition
The floor of a number in a sorted array is the greatest element 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 this definition exactly, while 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
- Thinking floor is strictly less than number
- Mixing floor with average or median
Solution
Step 1: Check Go function syntax
In Go, functions are declared withfunc, parameter types follow names, and return type is after parameters.Step 2: Validate each option
func floor(arr []int, x int) int { /* code */ } uses correct Go syntax:func floor(arr []int, x int) int. func floor(arr int[], x int) int { /* code */ } uses incorrect array syntax, C uses wrong keyword, D has wrong return type.Final Answer:
func floor(arr []int, x int) int { /* code */ } -> Option BQuick Check:
Go func syntax = func floor(arr []int, x int) int { /* code */ } [OK]
- Using 'function' keyword instead of 'func'
- Incorrect array type syntax
- Wrong return type declaration
arr := []int{1, 3, 5, 7, 9}
x := 6
floor := -1
for _, val := range arr {
if val <= x {
floor = val
} else {
break
}
}
fmt.Println(floor)Solution
Step 1: Trace the loop over array elements
The loop checks each element: 1 ≤ 6 (floor=1), 3 ≤ 6 (floor=3), 5 ≤ 6 (floor=5), 7 ≤ 6? No, break loop.Step 2: Determine final floor value
Last floor assigned is 5, so printed output is 5.Final Answer:
5 -> Option AQuick Check:
Floor of 6 in arr = 5 [OK]
- Assuming floor equals x if not present
- Not breaking loop early
- Confusing floor with ceil
x in a sorted array arr:
func ceil(arr []int, x int) int {
ceil := -1
for _, val := range arr {
if val >= x {
ceil = val
}
}
return ceil
}Solution
Step 1: Analyze loop behavior for ceil
The loop updates ceil whenever val ≥ x, but does not stop after first valid ceil, so it ends with the last such value, which may not be the smallest ceil.Step 2: Identify correct fix
To find the smallest ceil, the loop should break immediately after first val ≥ x is found.Final Answer:
It does not break after finding first valid ceil -> Option DQuick Check:
Ceil needs first ≥ x, break loop [OK]
- Not breaking loop early
- Confusing ceil with floor logic
- Incorrect initialization of ceil
arr := []int{2, 4, 6, 8, 10} and a number x = 5, write a Go function that returns both floor and ceil of x. What is the correct output of floor and ceil?Solution
Step 1: Find floor of 5 in arr
Floor is greatest element ≤ 5. Elements ≤ 5 are 2 and 4, greatest is 4.Step 2: Find ceil of 5 in arr
Ceil is smallest element ≥ 5. Elements ≥ 5 are 6, 8, 10, smallest is 6.Final Answer:
Floor: 4, Ceil: 6 -> Option CQuick Check:
Floor ≤ x ≤ Ceil = 4 ≤ 5 ≤ 6 [OK]
- Swapping floor and ceil values
- Assuming floor or ceil equals x if not present
- Returning -1 when valid floor or ceil exists
Ready to prove it?
Pass the test and this pattern moves unknown
