// floor of N = the largest element in the array smaller than or equal to N
function find_the_floor(arr: number[], N: number) {
const size: number = arr.length;
if (N >= arr[size - 1]) {
return size - 1;
}
if (N < arr[0]) {
return -1;
}
for (let i: number = 1; i < size; i++) {
if (arr[i] > N) {
return i - 1;
}
}
return -1;
}
const arr: number[] = [1, 2, 7, 8, 14, 19, 20, 24, 28];
const N: number = 17;
const index: number = find_the_floor(arr, N);
if (index == -1) {
console.log("The floor doesn\'t exist in array");
}
else {
console.log("The floor of " + N + " is " + arr[index]);
}
/*
run:
"The floor of 17 is 14"
*/