educative.io

Is not an edge case missing?

For example, looking at “searchCeilingOfANumber”, why can’t we add an additional “if” checking the other boundary?

public static int searchCeilingOfANumber(int[] arr, int key) {
    if (arr.length == 0) return -1; // trivial edge case
    
    // solution's condition
    if (key > arr[arr.length - 1]) // if the 'key' is bigger than the biggest element
      return -1;

    // my new condition
    if (key <= arr[0]) // if the 'key' is smaller or equal than the smallest element, return that element
      return arr[0];

Hi, the condition that you have mentioned is definitely a useful edge case. Although you will need to return 0 instead of arr[0] as we need the index of the ceiling and not the value.

You are right. I should have written, “return 0” (I cannot edit my question). Thanks for the answer.

1 Like