Floor and Ceil in Sorted Array in DSA Javascript - 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.
function floorAndCeil(arr, x) {
let left = 0, right = arr.length - 1;
let floor = -1, ceil = -1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] === x) {
floor = arr[mid];
ceil = arr[mid];
break;
} else if (arr[mid] < x) {
floor = arr[mid];
left = mid + 1;
} else {
ceil = arr[mid];
right = mid - 1;
}
}
return { floor, ceil };
}
This code finds the floor (largest value ≤ x) and ceil (smallest value ≥ 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: About log base 2 of n times, where n is the array length.
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 more step.
Time Complexity: O(log n)
This means the time to find floor and ceil grows slowly, even for large arrays.
[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.
Understanding this helps you quickly find values in sorted data, a common task in coding challenges and real projects.
"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 with options
The greatest element less than or equal to the number matches this definition exactly, while others describe ceil or incorrect 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 must be strictly less than number
- Mixing floor with average or median
x in a sorted array arr using binary search?Solution
Step 1: Understand binary search for floor
Binary search uses start and end pointers, calculates mid, and checks ifarr[mid] <= xto move start or end accordingly.Step 2: Evaluate options
Use a while loop withstart = 0,end = arr.length - 1, update mid and checkarr[mid] <= xcorrectly describes binary search logic for floor. Options A, B, and C are incorrect or irrelevant.Final Answer:
Use a while loop with start=0, end=arr.length-1, update mid and check arr[mid] ≤ x -> Option DQuick Check:
Binary search with arr[mid] ≤ x finds floor [OK]
- Using linear search instead of binary search
- Using indexOf which only finds exact matches
- Misusing Math.floor which works on numbers, not arrays
const arr = [1, 3, 5, 7, 9];
function findFloor(arr, x) {
let start = 0, end = arr.length - 1, floor = -1;
while (start <= end) {
let mid = Math.floor((start + end) / 2);
if (arr[mid] <= x) {
floor = arr[mid];
start = mid + 1;
} else {
end = mid - 1;
}
}
return floor;
}
console.log(findFloor(arr, 6));Solution
Step 1: Trace binary search for floor of 6
Initial start=0, end=4, floor=-1. mid=2 (arr[2]=5) ≤ 6, floor=5, start=3. mid=3 (arr[3]=7) > 6, end=2. Loop ends.Step 2: Return floor value
Floor found is 5, which is the greatest element ≤ 6.Final Answer:
5 -> Option BQuick Check:
Floor of 6 in [1,3,5,7,9] is 5 [OK]
- Returning mid index instead of value
- Confusing floor with ceil
- Not updating floor variable correctly
x in a sorted array arr:function findCeil(arr, x) {
let start = 0, end = arr.length - 1, ceil = -1;
while (start <= end) {
let mid = Math.floor((start + end) / 2);
if (arr[mid] >= x) {
ceil = arr[mid];
end = mid + 1;
} else {
start = mid + 1;
}
}
return ceil;
}Solution
Step 1: Analyze binary search update for ceil
When arr[mid] ≥ x, we update ceil and must move end to mid - 1 to search left side for smaller ceil.Step 2: Identify incorrect update
The code incorrectly setsend = mid + 1, which moves end right, causing infinite loop or wrong result.Final Answer:
The line end = mid + 1; should be end = mid - 1; -> Option AQuick Check:
For ceil, move end left with end = mid - 1 [OK]
- Moving end pointer in wrong direction
- Confusing ceil logic with floor
- Incorrect initial ceil value
arr = [2, 4, 6, 8, 10], write a function to find both floor and ceil of a number x. What will be the floor and ceil of x = 5?Solution
Step 1: Understand floor and ceil definitions
Floor is greatest element ≤ x, ceil is smallest element ≥ x.Step 2: Find floor and ceil for x=5 in arr
Elements ≤ 5 are 2 and 4; greatest is 4 (floor). Elements ≥ 5 are 6, 8, 10; smallest is 6 (ceil).Final Answer:
Floor = 4, Ceil = 6 -> Option CQuick Check:
Floor ≤ 5 = 4, Ceil ≥ 5 = 6 [OK]
- Swapping floor and ceil values
- Assuming floor or ceil equals x if not present
- Choosing wrong boundary elements
Ready to prove it?
Pass the test and this pattern moves unknown
