What if you could find the closest number in a sorted list instantly without checking each one?
Why Floor and Ceil in Sorted Array in DSA C?
Imagine you have a long list of sorted numbers, like heights of students in a class. You want to find the closest height that is just smaller or equal (floor) or just bigger or equal (ceil) to a given height. Doing this by checking each number one by one is like searching for a friend in a crowd by asking every person their name.
Checking each number one by one is slow and tiring, especially if the list is very long. It's easy to make mistakes or miss the closest number because you have to remember many values. This manual search wastes time and energy.
Using the floor and ceil concept with a sorted array lets you quickly jump to the closest numbers without checking all. It uses a smart way to split the list and find the answer fast, like using a map to find your friend's location instead of asking everyone.
int floor = -1; for (int i = 0; i < n; i++) { if (arr[i] <= x) floor = arr[i]; }
int floor = -1, left = 0, right = n - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] <= x) { floor = arr[mid]; left = mid + 1; } else { right = mid - 1; } }
This concept enables lightning-fast searches for closest smaller or bigger values in sorted lists, saving time and effort.
When booking a flight, you want the closest available departure time before or after your preferred time. Floor and ceil help find these times quickly from a sorted schedule.
Manual search is slow and error-prone for large sorted lists.
Floor and ceil use smart splitting (binary search) to find closest values fast.
This method saves time and helps in real-world tasks like scheduling and pricing.
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
