The floor of a number is the greatest element in the array that is less than or equal to that number.
Click to reveal answer
beginner
What is the ceil of a number in a sorted array?
The ceil of a number is the smallest element in the array that is greater than or equal to that number.
Click to reveal answer
intermediate
Why is binary search useful for finding floor and ceil in a sorted array?
Binary search helps find floor and ceil efficiently by repeatedly dividing the search space in half, reducing time complexity to O(log n).
Click to reveal answer
intermediate
If the target number is smaller than all elements in the array, what is the floor and ceil?
Floor does not exist (no element ≤ target), ceil is the smallest element in the array.
Click to reveal answer
intermediate
If the target number is larger than all elements in the array, what is the floor and ceil?
Floor is the largest element in the array, ceil does not exist (no element ≥ target).
Click to reveal answer
What does the floor of 7 mean in the sorted array [1, 3, 5, 8, 10]?
AThe number 7 itself
BThe smallest number ≥ 7
CThe largest number ≤ 7
DThe smallest number in the array
✗ Incorrect
Floor is the greatest element less than or equal to the target, so here it is 5.
What is the ceil of 6 in the sorted array [2, 4, 6, 8, 10]?
A4
B10
C8
D6
✗ Incorrect
Ceil is the smallest element greater than or equal to 6, which is 6 itself.
If the target is 0 and the array is [5, 10, 15], what is the floor?
A0
BNo floor exists
C5
D15
✗ Incorrect
No element is less than or equal to 0, so floor does not exist.
Which algorithm is best to find floor and ceil in a sorted array efficiently?
ABinary search
BLinear search
CBubble sort
DInsertion sort
✗ Incorrect
Binary search reduces the search space quickly, making it efficient for this task.
If the target is 20 and the array is [1, 5, 10, 15], what is the ceil?
ANo ceil exists
B20
C1
D15
✗ Incorrect
No element is greater than or equal to 20, so ceil does not exist.
Explain how to find the floor and ceil of a number in a sorted array using binary search.
Think about how to move left or right in the array to find closest smaller or larger elements.
You got /4 concepts.
Describe what happens when the target number is smaller than all elements or larger than all elements in the array regarding floor and ceil.
Consider the array boundaries and what floor and ceil mean.
You got /4 concepts.
Practice
(1/5)
1. What does the floor of a number mean in a sorted array context?
easy
A. The greatest element less than or equal to the number
B. The smallest element greater than or equal to the number
C. The average of all elements less than the number
D. The smallest element strictly less than the number
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 A
Quick Check:
Floor = greatest ≤ number [OK]
Hint: Floor is always ≤ number, pick greatest such element [OK]
Common Mistakes:
Confusing floor with ceil
Thinking floor must be strictly less than number
Mixing floor with average or median
2. Which of the following is the correct JavaScript syntax to find the floor of a number x in a sorted array arr using binary search?
easy
A. Use Math.floor(x) directly on array
B. Use a for loop from 0 to arr.length and return first element greater than x
C. Use arr.indexOf(x) to find floor
D. Use a while loop with start = 0, end = arr.length - 1, update mid and check arr[mid] <= x
Solution
Step 1: Understand binary search for floor
Binary search uses start and end pointers, calculates mid, and checks if arr[mid] <= x to move start or end accordingly.
Step 2: Evaluate options
Use a while loop with start = 0, end = arr.length - 1, update mid and check arr[mid] <= x correctly 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 D
Quick Check:
Binary search with arr[mid] ≤ x finds floor [OK]
Hint: Binary search with arr[mid] ≤ x finds floor efficiently [OK]
Common Mistakes:
Using linear search instead of binary search
Using indexOf which only finds exact matches
Misusing Math.floor which works on numbers, not arrays
3. What is the output of the following JavaScript code?
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));