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 taken change as the array size grows?
Analyze the time complexity of the following code snippet.
int floorCeil(int arr[], int n, int x, int *floor, int *ceil) {
int low = 0, high = n - 1;
*floor = -1; *ceil = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == x) {
*floor = arr[mid];
*ceil = arr[mid];
return 0;
} else if (arr[mid] < x) {
*floor = arr[mid];
low = mid + 1;
} else {
*ceil = arr[mid];
high = mid - 1;
}
}
return 0;
}
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 log2(n) times, where n is the array size.
Each step cuts the search space in half, so the number of steps grows slowly as the array gets bigger.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 |
| 100 | About 7 |
| 1000 | About 10 |
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 becomes very large.
[X] Wrong: "Since we check elements one by one, the time is O(n)."
[OK] Correct: The code uses binary search, which skips many elements by dividing the search space, so it does not check every element.
Knowing how to quickly find floor and ceil using binary search shows you understand efficient searching in sorted data, 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 in a sorted array?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, others describe ceil or incorrect concepts.Final Answer:
The greatest element less than or equal to the number -> Option DQuick Check:
Floor = greatest ≤ number [OK]
- Confusing floor with ceil
- Thinking floor is strictly less than number
- Mixing floor with average or median
n?Solution
Step 1: Understand middle index calculation in binary search
For array indices 0 to n-1, middle is (0 + n-1) / 2 or equivalent.Step 2: Analyze options for correct syntax and efficiency
int mid = (0 + (n - 1)) >> 1; uses bitwise right shift for division by 2, which is efficient and correct for integer mid calculation.Final Answer:
int mid = (0 + (n - 1)) >> 1; -> Option CQuick Check:
Mid = (start + end) >> 1 [OK]
- Using n instead of n-1 as end index
- Forgetting parentheses causing wrong precedence
- Using floating division instead of integer
arr = {1, 3, 5, 7, 9}, what is the floor of 6?Solution
Step 1: Identify elements less than or equal to 6
Elements ≤ 6 are 1, 3, 5.Step 2: Find greatest among these
The greatest element ≤ 6 is 5.Final Answer:
5 -> Option BQuick Check:
Floor(6) = 5 [OK]
- Choosing 7 which is greater than 6
- Assuming floor equals the number if not present
- Returning -1 incorrectly when floor exists
int ceil = -1; for(int i=0; i= x) { ceil = arr[i]; break; } }. What is the main issue?Solution
Step 1: Understand the code logic
The code returns the first element in array ≥ x, which is correct if array is sorted.Step 2: Identify assumptions and issues
If array is not sorted, first element ≥ x may not be the smallest such element, so ceil is incorrect.Final Answer:
It may return wrong value if array is not sorted -> Option AQuick Check:
Ceil requires sorted array for correctness [OK]
- Ignoring array sorting requirement
- Confusing ceil with floor
- Not checking for empty array before loop
arr = {2, 4, 6, 8, 10}. If you want to find the ceil of 11 using binary search, what should be the returned value?Solution
Step 1: Understand ceil definition and array elements
Ceil is smallest element ≥ target. Here, target is 11, which is greater than all elements.Step 2: Check if any element ≥ 11 exists
No element in array ≥ 11, so ceil does not exist, return -1.Final Answer:
-1 -> Option AQuick Check:
No element ≥ 11 means ceil = -1 [OK]
- Returning last element as ceil incorrectly
- Returning target itself if not found
- Not handling no ceil case properly
Ready to prove it?
Pass the test and this pattern moves unknown
