Bird
Raised Fist0
DSA Cprogramming~5 mins

Floor and Ceil in Sorted Array in DSA C - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Floor and Ceil in Sorted Array
O(log n)
Understanding Time 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?

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

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
10About 4
100About 7
1000About 10

Pattern observation: Doubling the input size adds only one more step.

Final Time Complexity

Time Complexity: O(log n)

This means the time to find floor and ceil grows slowly, even if the array becomes very large.

Common Mistake

[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.

Interview Connect

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.

Self-Check

"What if the array was not sorted? How would the time complexity change?"

Practice

(1/5)
1. What is the floor of a number in a sorted array?
easy
A. The smallest element strictly less than 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 greatest element less than or equal to the number

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    The greatest element less than or equal to the number -> Option D
  4. Quick Check:

    Floor = greatest ≤ number [OK]
Hint: Floor is always ≤ number, pick greatest such element [OK]
Common Mistakes:
  • Confusing floor with ceil
  • Thinking floor is strictly less than number
  • Mixing floor with average or median
2. Which of the following is the correct C syntax to find the middle index in binary search for array of size n?
easy
A. int mid = (0 + n) / 2;
B. int mid = 0 + n - 1 / 2;
C. int mid = (0 + (n - 1)) >> 1;
D. int mid = n / 2;

Solution

  1. Step 1: Understand middle index calculation in binary search

    For array indices 0 to n-1, middle is (0 + n-1) / 2 or equivalent.
  2. 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.
  3. Final Answer:

    int mid = (0 + (n - 1)) >> 1; -> Option C
  4. Quick Check:

    Mid = (start + end) >> 1 [OK]
Hint: Use (start + end) >> 1 for efficient mid calculation [OK]
Common Mistakes:
  • Using n instead of n-1 as end index
  • Forgetting parentheses causing wrong precedence
  • Using floating division instead of integer
3. Given the sorted array arr = {1, 3, 5, 7, 9}, what is the floor of 6?
medium
A. 7
B. 5
C. 6
D. -1

Solution

  1. Step 1: Identify elements less than or equal to 6

    Elements ≤ 6 are 1, 3, 5.
  2. Step 2: Find greatest among these

    The greatest element ≤ 6 is 5.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    Floor(6) = 5 [OK]
Hint: Floor is largest ≤ target in sorted array [OK]
Common Mistakes:
  • Choosing 7 which is greater than 6
  • Assuming floor equals the number if not present
  • Returning -1 incorrectly when floor exists
4. Consider this code snippet to find ceil in a sorted array int ceil = -1; for(int i=0; i= x) { ceil = arr[i]; break; } }. What is the main issue?
medium
A. It may return wrong value if array is not sorted
B. It does not handle empty array
C. It returns first element ≥ x, which is correct for ceil
D. It finds floor instead of ceil

Solution

  1. Step 1: Understand the code logic

    The code returns the first element in array ≥ x, which is correct if array is sorted.
  2. 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.
  3. Final Answer:

    It may return wrong value if array is not sorted -> Option A
  4. Quick Check:

    Ceil requires sorted array for correctness [OK]
Hint: Ceil logic needs sorted array to work correctly [OK]
Common Mistakes:
  • Ignoring array sorting requirement
  • Confusing ceil with floor
  • Not checking for empty array before loop
5. You have a sorted array arr = {2, 4, 6, 8, 10}. If you want to find the ceil of 11 using binary search, what should be the returned value?
hard
A. -1
B. 11
C. 10
D. Error

Solution

  1. Step 1: Understand ceil definition and array elements

    Ceil is smallest element ≥ target. Here, target is 11, which is greater than all elements.
  2. Step 2: Check if any element ≥ 11 exists

    No element in array ≥ 11, so ceil does not exist, return -1.
  3. Final Answer:

    -1 -> Option A
  4. Quick Check:

    No element ≥ 11 means ceil = -1 [OK]
Hint: If target > max element, ceil is -1 (not found) [OK]
Common Mistakes:
  • Returning last element as ceil incorrectly
  • Returning target itself if not found
  • Not handling no ceil case properly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard