CT907

The First Occurrence: Find the Leftmost Index of a Target

EasyL3 · Syntax SurferAcceptance: 0.0%XP: 20

In the Grand Library of Prime City, the Head Librarian needs to find the first occurrence of a specific book in a sorted shelf. Multiple copies of the same book may exist, and the Librarian wants the leftmost one ÔÇö the one that was placed first. "This is the lower bound search," the Librarian explains. "Use binary search, but when you find the target, don't stop ÔÇö keep searching to the left. The first occurrence is the leftmost position where the target appears." Given a sorted array of N integers and a target value T, find the index of the first occurrence of T (the leftmost 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: 1 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 lower-bound first-occurrence search
Loading...
Test Cases:No test cases
No test cases available.
The First Occurrence: Find the Leftmost Index of a Target - EASY Coding Problem | CodeTikki