// floor of N = the largest element in the array smaller than or equal to N
class Program {
private static int find_the_floor(int[] arr, int N) {
int size = arr.length;
if (N >= arr[size - 1]) {
return size - 1;
}
if (N < arr[0]) {
return -1;
}
for (int i = 1; i < size; i++) {
if (arr[i] > N) {
return i - 1;
}
}
return -1;
}
public static void main(String[] args) {
int[] arr = {1, 2, 7, 8, 14, 19, 20, 24, 28};
int N = 17;
int index = find_the_floor(arr, N);
if (index == -1) {
System.out.print("The floor doesn't exist in array");
}
else {
System.out.print("The floor of " + N + " is " + arr[index]);
}
}
}
/*
run:
The floor of 17 is 14
*/