Floor and Ceil in Sorted Array in DSA C# - Time & Space Complexity
We want to understand 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.
public static int Floor(int[] arr, int target) {
int left = 0, right = arr.Length - 1;
int floor = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) return arr[mid];
if (arr[mid] < target) {
floor = arr[mid];
left = mid + 1;
} else {
right = mid - 1;
}
}
return floor;
}
This code finds the floor value of a target 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: The loop runs about log base 2 of n times, where n is the array size.
Each step cuts the array size roughly in half, so the number of steps grows slowly as the array gets bigger.
| 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 or ceil grows slowly, even if the array gets very large.
[X] Wrong: "Since we check elements one by one, the time is O(n)."
[OK] Correct: The code does not check every element; it jumps around by halving the search space, so it runs much faster.
Understanding this time complexity helps you explain why binary search is efficient and preferred for sorted data.
"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 that number.Step 2: Compare options with definition
The greatest element less than or equal to the number matches the definition exactly. Options A, B, and D do not match the floor concept.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 the number
- Mixing floor with average or median
Solution
Step 1: Check method return type and parameters
The function should return an integer (the floor value) and accept an integer array and an integer target.Step 2: Validate each option
int FindFloor(int[] arr, int target) matches the correct signature: returns int, takes int[] and int. void FindFloor(int[] arr, int target) returns void, so no result. int FindFloor(int target, int[] arr) swaps parameters order (less common but possible). int FindFloor(List<int> arr, string target) uses wrong types (List<int> and string target).Final Answer:
int FindFloor(int[] arr, int target) -> Option BQuick Check:
Return int, params (int[], int) [OK]
- Using void return type
- Swapping parameter order incorrectly
- Using wrong parameter types
int[] arr = {1, 3, 5, 7, 9};
int target = 6;
int floor = -1;
int left = 0, right = arr.Length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
floor = arr[mid];
break;
} else if (arr[mid] < target) {
floor = arr[mid];
left = mid + 1;
} else {
right = mid - 1;
}
}
Console.WriteLine(floor);Solution
Step 1: Trace binary search for floor of 6
Initial array: [1,3,5,7,9]. Target=6. Start left=0, right=4, floor=-1.
mid=2 (arr[2]=5) < 6, floor=5, left=3.
mid=3 (arr[3]=7) > 6, right=2.
Loop ends as left > right.Step 2: Final floor value
Floor is 5, the greatest element ≤ 6.Final Answer:
5 -> Option AQuick Check:
Floor(6) in [1,3,5,7,9] = 5 [OK]
- Returning 7 instead of 5
- Confusing floor with ceil
- Not updating floor inside loop
int Ceil(int[] arr, int target) {
int left = 0, right = arr.Length - 1;
int ceil = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return arr[mid];
} else if (arr[mid] > target) {
ceil = arr[mid];
right = mid + 1;
} else {
left = mid + 1;
}
}
return ceil;
}Solution
Step 1: Analyze right pointer update in binary search
When arr[mid] > target, we want to search left half, so right should move to mid - 1, not mid + 1.Step 2: Identify impact of wrong update
Using right = mid + 1 moves right pointer incorrectly, causing infinite loop or wrong result.Final Answer:
right should be updated to mid - 1, not mid + 1 -> Option CQuick Check:
Correct binary search halves pointers [OK]
- Updating right pointer incorrectly
- Confusing ceil with floor logic
- Not returning correct default value
arr = {2, 4, 6, 8, 10} and a target 5, write a C# method to return both floor and ceil values as a tuple. What should the method return?Solution
Step 1: Identify floor and ceil for target 5 in array
Array: [2,4,6,8,10]. Floor is greatest ≤ 5, which is 4. Ceil is smallest ≥ 5, which is 6.Step 2: Confirm tuple return
The method should return (floor, ceil) = (4, 6).Final Answer:
(4, 6) -> Option AQuick Check:
Floor=4, Ceil=6 for target 5 [OK]
- Returning target itself when not in array
- Swapping floor and ceil values
- Returning -1 when values exist
Ready to prove it?
Pass the test and this pattern moves unknown
