-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloor_Ceil_Binary.java
More file actions
41 lines (33 loc) · 1.15 KB
/
Floor_Ceil_Binary.java
File metadata and controls
41 lines (33 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.*;
public class Floor_Ceil_Binary {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter " + n + " sorted denominations:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.print("Enter the denomination to search for: ");
int d = sc.nextInt();
int floor = -1, ceil = -1;
int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == d) {
floor = ceil = arr[mid];
break;
} else if (arr[mid] < d) {
floor = arr[mid];
low = mid + 1;
} else {
ceil = arr[mid];
high = mid - 1;
}
}
System.out.println("\nOutput:");
System.out.println("Floor: " + floor);
System.out.println("Ceil: " + ceil);
}
}