#K79397. Find the First Occurrence in a Sorted Array
Find the First Occurrence in a Sorted Array
Find the First Occurrence in a Sorted Array
Given a sorted array of n integers and a target value x, you are required to find the index of the first occurrence of x in the array. If x is not present, output -1. The solution must be efficient, leveraging a binary search approach that correctly handles arrays containing duplicate values. The index is considered with 0-based numbering.
inputFormat
The input is read from stdin and consists of two parts:
- The first line contains two space-separated integers: n (the number of elements) and x (the target value).
- The second line contains n space-separated integers in non-decreasing order.
outputFormat
Output a single integer (written to stdout), which is the index (0-based) of the first occurrence of x in the array. If x is not found, output -1.
## sample10 4
2 4 4 4 7 9 10 14 16 18
1
</p>