Practice arena
C++17·2 s·128 MB
SearchingFoundation

First occurrence

Return the first zero-based index of target in a non-decreasing array containing duplicates, or -1 if absent.

Input

  • The first line contains T.
  • Each case contains n and target, followed by n sorted integers.

Output

Print one index per test case.

Technique

lower bound

Target

O(log n)

Constraints & sample
  • 1 ≤ n ≤ 200,000
  • The array is sorted in non-decreasing order

Sample input

3
6 2
1 2 2 2 4 7
5 9
1 3 5 7 9
4 6
1 2 3 4

Expected output

1
4
-1
Review the concept

Before you submit

  • Test the smallest valid input and one awkward edge case.
  • Keep the solution within O(log n).
  • Match spacing and line breaks exactly before checking 9 hidden cases.
main.cpp
Draft saved locally · 463 / 30,000
Ln 1, Col 1Spaces: 4UTF-8LFC++17

Visible testcases

Editable and stored only in this browser. Hidden judge cases never change.

Checking your session…

Run checks Sample · Submit checks 9 hidden cases

Run the sample first, then send your solution through 9 hidden cases.

Code runs in an isolated Judge0 environment. Drafts and custom visible cases stay on this device; submission metadata and progress sync to MongoDB.