Floor and Ceil in a Sorted Array
Given a non-empty integer array nums sorted in non-decreasing order and a non-negative integer target, return [floor, ceil]. The floor is the greatest array…
Given a non-empty integer array `nums` sorted in non-decreasing order and a non-negative integer `target`, return `[floor, ceil]`. The floor is the greatest array value at most `target`; the ceil is the smallest array value at least `target`. Return `-1` for either value when it does not exist.
- `1 <= nums.length <= 10^4` - `0 <= nums[i], target <= 10^9` - `nums` is sorted in non-decreasing order.
Example 1: input nums = [1,2,4,6,8], target = 5; output [4,6]. 4 is the greatest value below 5 and 6 is the smallest above it.
Example 2: input nums = [1,2,4,6,8], target = 4; output [4,4]. An exact match is both floor and ceil.
Example 3: input nums = [2,4,6], target = 1; output [-1,2]. No floor exists, while 2 is the smallest qualifying ceil.
Category: Binary Search. Topic: binary-search. Difficulty: Easy. Patterns: binary-search.