Ceiling and Floor of a number for array in descending order (Java)

50 Views Asked by At

This is the program I wrote to find ceiling and floor of a Number.

public class BinarySearchAlgorithm {
    public static void main(String[] args) {
        int[] arr = {10, 9, 8, 7, 4, 3, 2, 1};
        int target = 6;
        System.out.println(ceilingOfNumber(arr, target));
        System.out.println(floorOfNumber(arr, target));
    }
    
    static int ceilingOfNumber(int[] arr, int target) {
        int start = 0;
        int end = arr.length -1;
        boolean isAscending = arr[start] < arr[end];

        while (start <= end) {
            int mid = start + (end - start) / 2;

            if (target == arr[mid]) {
                return mid;
            }

            if (isAscending) {
                if (target > arr[mid]) {
                    start = mid + 1;
                } else {
                    end = mid -1;
                }
            } else {
                if (target > arr[mid]) {
                    end = mid - 1;
                } else {
                    start = mid + 1;
                }
            }
        }

        return start;
    }

    static int floorOfNumber(int[] arr, int target) {
        int start = 0;
        int end = arr.length -1;
        boolean isAscending = arr[start] < arr[end];

        while (start <= end) {
            int mid = start + (end - start) / 2;

            if (target == arr[mid]) {
                return mid;
            }

            if (isAscending) {
                if (target > arr[mid]) {
                    start = mid + 1;
                } else {
                    end = mid -1;
                }
            } else {
                if (target > arr[mid]) {
                    end = mid - 1;
                } else {
                    start = mid + 1;
                }
            }
        }

        return end;
    }
}

I am trying to get the index value for the ceiling of a number and floor of a number.

Clearly from the Array -

ceilingOfNumber should return : 3
floorOfNumber should return : 4

but instead,

ceilingOfNumber is returning : 4
floorOfNumber is returning : 3

I tried to debug it and I also did code-tracing but couldn't find any solution.

1

There are 1 best solutions below

0
Partha Sarathi On

The problem with your implemented code is that, you are always returning start index for celling value and end index for floor value, even when you are considering both Ascending and Descending order array.

Let's understand the issue. As per the logic, if the target number not present in the actual array, the loop will only terminate when end < start. Now, if it's an ascending order array, end index will have lesser value than start index. Because of that, end index for floor and start index for ceiling have no issue. But for descending order array, end < start implies that start index has lesser value than end index. In that case ceiling should be end index and floor should be start index.

Your can change your logic as -
Ceiling : return isAscending ? start : end;
Floor : return isAscending ? end : start;