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

First matching index

Find the first zero-based index of target in an unsorted array. Return -1 when target is absent.

Input

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

Output

Print the first matching index or -1 for each case.

Technique

left-to-right scan

Target

O(n)

Constraints & sample
  • 1 ≤ n ≤ 200,000
  • The array is not guaranteed to be sorted

Sample input

3
5 7
4 7 1 7 9
4 8
8 2 8 3
3 5
1 2 3

Expected output

1
0
-1
Review the concept

Before you submit

  • Test the smallest valid input and one awkward edge case.
  • Keep the solution within O(n).
  • Match spacing and line breaks exactly before checking 8 hidden cases.
main.cpp
Draft saved locally · 482 / 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 8 hidden cases

Run the sample first, then send your solution through 8 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.