CT908

The Last Occurrence: Find the Rightmost Index of a Target

EasyL3 · Syntax SurferAcceptance: 0.0%XP: 20

In the Grand Library of Prime City, the Head Librarian now needs the last copy of a specific book ÔÇö the rightmost one on the sorted shelf. This is the upper bound search: find the rightmost occurrence of the target. "When you find the target, keep searching to the right," the Librarian says. "The last occurrence is the rightmost position where the target appears." Given a sorted array of N integers and a target value T, find the index of the last occurrence of T (the rightmost index). If T is not present, output -1. Constraints: 1 <= N <= 10^5, -10^9 <= array[i], T <= 10^9, array is sorted in non-decreasing order Input: 7 1 2 2 2 3 4 5 2 Output: 3 Input: 5 1 3 5 7 9 4 Output: -1

Constraints:

1 <= N <= 10^5, -10^9 <= array[i], T <= 10^9, sorted non-decreasing

Tags:

binary-search upper-bound last-occurrence search
Loading...
Test Cases:No test cases
No test cases available.
The Last Occurrence: Find the Rightmost Index of a Target - EASY Coding Problem | CodeTikki