-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathceiling_of_a_number.js
More file actions
30 lines (26 loc) · 919 Bytes
/
Copy pathceiling_of_a_number.js
File metadata and controls
30 lines (26 loc) · 919 Bytes
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
const search_ceiling_of_a_number = function(arr, key) {
let n = arr.length;
if (key > arr[n-1])
return -1;
let start = 0;
let end = n - 1;
//NOTE: <= and not <
while (start <= end) {
//NOTE: do not forget start+ for the second half of the array. For the first half, start will be 0
let center = Math.floor(start + (end-start) / 2);
if (key < arr[center]) {
end = center - 1;
}
else if (key > arr[center]) {
start = center + 1;
}
else {
return center;
}
}
return start; //by definition, this is the next biggest
};
console.log(search_ceiling_of_a_number([4, 6, 10], 6))
console.log(search_ceiling_of_a_number([1, 3, 8, 10, 15], 12))
console.log(search_ceiling_of_a_number([4, 6, 10], 17))
console.log(search_ceiling_of_a_number([4, 6, 10], -1))