Floor and Ceil in Sorted Array in DSA Java - Time & Space Complexity
We want to know how fast we can find the floor and ceil values in a sorted array.
How does the time needed change as the array gets bigger?
Analyze the time complexity of the following code snippet.
public int floor(int[] arr, int x) {
int left = 0, right = arr.length - 1;
int floor = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == x) return arr[mid];
if (arr[mid] < x) {
floor = arr[mid];
left = mid + 1;
} else {
right = mid - 1;
}
}
return floor;
}
This code finds the floor 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: The loop 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 or ceil grows slowly, even if the array gets very large.
[X] Wrong: "Since we look at elements one by one, it takes O(n) time."
[OK] Correct: The code does not check every element; it jumps around by halving the search space, so it is 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 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
- Thinking floor is strictly less than the number
- Mixing floor with average or median
Solution
Step 1: Check Java method signature syntax
Java methods must specify access modifier, return type, method name, and parameter types.Step 2: Validate each option
public static int findFloor(int[] arr, int target) is correct: public static int findFloor(int[] arr, int target). int findFloor(int arr[], target) misses access modifier and parameter type for target. static int findFloor(int[] arr, int target) throws Exception unnecessarily throws Exception. public int findFloor(int[] arr, int target, int size) has extra parameter 'size' not needed.Final Answer:
public static int findFloor(int[] arr, int target) -> Option DQuick Check:
Correct Java method signature = public static int findFloor(int[] arr, int target) [OK]
- Omitting access modifier
- Missing parameter types
- Adding unnecessary throws clause
int[] arr = {1, 3, 5, 7, 9} and target = 6, what is the floor and ceil of 6?Solution
Step 1: Identify floor of 6 in array
Floor is greatest element ≤ 6. Elements ≤ 6 are 1, 3, 5. Greatest is 5.Step 2: Identify ceil of 6 in array
Ceil is smallest element ≥ 6. Elements ≥ 6 are 7, 9. Smallest is 7.Final Answer:
Floor = 5, Ceil = 7 -> Option CQuick Check:
Floor ≤ 6 = 5, Ceil ≥ 6 = 7 [OK]
- Choosing floor greater than target
- Choosing ceil less than target
- Assuming target must be in array
public static int findFloor(int[] arr, int target) {
int low = 0, high = arr.length - 1;
int floor = -1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target) return arr[mid];
else if (arr[mid] < target) {
floor = arr[mid];
low = mid + 1;
} else {
high = mid - 1;
}
}
return floor;
}What is the bug if the array is empty?
Solution
Step 1: Analyze behavior with empty array
If array is empty, arr.length = 0, so high = -1. Condition low <= high is false initially, so loop never runs.Step 2: Check return value when loop doesn't run
Variable floor initialized to -1 and returned as is, indicating no floor found.Final Answer:
Returns -1 correctly as floor does not exist -> Option AQuick Check:
Empty array returns -1 floor [OK]
- Assuming mid calculation causes error on empty array
- Expecting exception instead of -1 return
- Missing initialization of floor variable
int[] arr = {2, 4, 6, 8, 10}. You want to find the floor and ceil for target = 1 and target = 11. What are the correct floor and ceil values for these targets?Solution
Step 1: Find floor and ceil for target = 1
Floor is greatest element ≤ 1. No elements ≤ 1, so floor = -1 (not found). Ceil is smallest element ≥ 1, which is 2.Step 2: Find floor and ceil for target = 11
Floor is greatest element ≤ 11, which is 10. Ceil is smallest element ≥ 11, none exists, so ceil = -1.Final Answer:
For 1: Floor = -1, Ceil = 2; For 11: Floor = 10, Ceil = -1 -> Option BQuick Check:
Floor and ceil outside array bounds handled with -1 [OK]
- Returning array ends incorrectly as floor or ceil
- Confusing floor and ceil for out-of-bound targets
- Assuming floor or ceil always exists
Ready to prove it?
Pass the test and this pattern moves unknown
