Last Occurrence in a Sorted Array
Given a non-decreasing integer array nums and an integer target, return the index of the last occurrence of target. Return -1 when the target is absent.
Given a non-decreasing integer array `nums` and an integer `target`, return the index of the last occurrence of `target`. Return `-1` when the target is absent.
- `1 <= nums.length <= 10^5` - `-10^9 <= nums[i], target <= 10^9` - `nums` is sorted in non-decreasing order.
Example 1: input nums = [1,2,2,2,3], target = 2; output 3. The final value equal to 2 is at index 3.
Example 2: input nums = [1,2,4,5], target = 3; output -1. The target does not occur.
Category: Binary Search. Topic: binary-search. Difficulty: Easy. Patterns: binary-search.