#K47242. Find the K-th Occurrence

    ID: 28155 Type: Default 1000ms 256MiB

Find the K-th Occurrence

Find the K-th Occurrence

You are given an array of integers and two more integers, X and K. Your task is to find the position (1-indexed) of the K-th occurrence of X in the array. If X does not appear K times in the array, output -1.

For each test case, you should determine the index (i+1) where the count of occurrences of (X) reaches (K) for the first time. Otherwise, if no such occurrence exists, print (-1).

inputFormat

The input is given from (stdin) and consists of multiple test cases. The first line contains a single integer (T) (the number of test cases). For each test case:

  • The first line contains an integer (n), the size of the array.
  • The second line contains (n) space-separated integers representing the array elements.
  • The third line contains two integers (X) and (K), where (X) is the integer to search and (K) is the occurrence number to find.

outputFormat

For each test case, print a single line containing one integer: the 1-indexed position of the K-th occurrence of (X) in the array, or (-1) if (X) does not occur at least (K) times.## sample

1
5
1 2 3 4 2
2 2
5

</p>