| 1 | package com.datastructures; |
| 2 | |
| 3 | public class CeilingFloor { |
| 4 | |
| 5 | static int findCeiling(int[] arr, int target) { |
| 6 | int start = 0, end = arr.length - 1; |
| 7 | while (start <= end) { |
| 8 | int mid = start + (end - start) / 2; |
| 9 | if (target < arr[mid]) { |
| 10 | end = mid - 1; |
| 11 | } else if (target > arr[mid]) { |
| 12 | start = mid + 1; |
| 13 | } else { |
| 14 | return arr[mid]; |
| 15 | } |
| 16 | } |
| 17 | return arr[start]; |
| 18 | } |
| 19 | |
| 20 | static int findFloor(int arr[], int target) { |
| 21 | int start = 0, end = arr.length - 1; |
| 22 | while (start <= end) { |
| 23 | int mid = start + (end - start) / 2; |
| 24 | if (target < arr[mid]) { |
| 25 | end = mid - 1; |
| 26 | } else if (target > arr[mid]) { |
| 27 | start = mid + 1; |
| 28 | } else { |
| 29 | return arr[mid]; |
| 30 | } |
| 31 | } |
| 32 | return end; |
| 33 | } |
| 34 | |
| 35 | public static void main(String[] args) { |
| 36 | int[] arr = {12, 18, 24, 30, 36, 42}; |
| 37 | System.out.println(findCeiling(arr, 18)); |
| 38 | System.out.println(findFloor(arr, 1)); |
| 39 | } |
| 40 | } |
nothing calls this directly
no outgoing calls
no test coverage detected