Floor and Ceil in Sorted Array in DSA C++ - 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.
int floorCeil(const vector<int>& arr, int x, int& floorVal, int& ceilVal) {
int low = 0, high = arr.size() - 1;
floorVal = -1; ceilVal = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == x) {
floorVal = ceilVal = arr[mid];
return mid;
} else if (arr[mid] < x) {
floorVal = arr[mid];
low = mid + 1;
} else {
ceilVal = arr[mid];
high = mid - 1;
}
}
return -1;
}
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 range each time.
- How many times: The loop runs about log₂(n) times, where n is the array size.
Each step cuts the search area 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 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 half the array each step, so it doesn't check every element.
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. Other options 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 or exact match only
Solution
Step 1: Analyze function parameters
The function needs the array, its size, and the target number to find floor.Step 2: Check each option's signature
int findFloor(int arr[], int n, int x); correctly uses array pointer, size, and target number. void findFloor(int arr[], int n, int x); returns void which is incorrect. int findFloor(int arr, int n, int x); uses int arr instead of array. int findFloor(int arr[], int x); misses size parameter.Final Answer:
int findFloor(int arr[], int n, int x); -> Option AQuick Check:
Correct signature includes array, size, target [OK]
- Omitting array size parameter
- Using wrong array type
- Returning void instead of int
int arr[] = {1, 3, 5, 7, 9};
int n = 5;
int x = 6;
int floor = -1;
for (int i = 0; i < n; i++) {
if (arr[i] <= x) floor = arr[i];
else break;
}
std::cout << floor;Solution
Step 1: Trace the loop and condition
Loop checks each element ≤ 6. Elements 1, 3, 5 satisfy condition, floor updates to 5. When arr[i] = 7, condition fails and loop breaks.Step 2: Determine final floor value
Floor remains 5 after loop ends.Final Answer:
5 -> Option CQuick Check:
Floor of 6 in array = 5 [OK]
- Choosing 6 which is not in array
- Choosing 7 which is > 6
- Not updating floor correctly
int findCeil(int arr[], int n, int x) {
int ceil = -1;
for (int i = 0; i < n; i++) {
if (arr[i] >= x) {
ceil = arr[i];
}
}
return ceil;
}Solution
Step 1: Understand the loop logic
The loop updates ceil whenever arr[i] ≥ x, but does not stop or check for smallest such element.Step 2: Identify the error
Because it keeps updating, it ends with the last element ≥ x, not the smallest. It should break or track minimum.Final Answer:
It does not return the smallest element ≥ x -> Option DQuick Check:
Ceil = smallest ≥ x, code returns largest ≥ x [OK]
- Not breaking loop after first valid ceil
- Updating ceil without checking if smaller
- Confusing floor and ceil logic
arr = {2, 4, 6, 8, 10}, what are the floor and ceil of x = 5? Choose the correct pair.Solution
Step 1: Find floor of 5 in array
Floor is greatest element ≤ 5. Elements ≤ 5 are 2 and 4, greatest is 4.Step 2: Find ceil of 5 in array
Ceil is smallest element ≥ 5. Elements ≥ 5 are 6, 8, 10, smallest is 6.Final Answer:
Floor = 4, Ceil = 6 -> Option BQuick Check:
Floor ≤ 5 = 4, Ceil ≥ 5 = 6 [OK]
- Assuming floor or ceil must equal x
- Choosing elements not in array
- Mixing floor and ceil values
Ready to prove it?
Pass the test and this pattern moves unknown
