A warehouse installs a single barcode scanner above a conveyor belt. As boxes pass beneath the scanner, it logs the item code of each box, in order, starting from scan position 0. The warehouse manager is auditing deliveries of one particular item code: for a list of ranks, she wants to know at which scan position the k-th delivery of that code passed the scanner.
n and x — the number of scanned entries and the target item code.n integers codes[0] codes[1] ... codes[n-1] — the scan log, in order.q — the number of queries.q integers k_1 k_2 ... k_q — for each query, the requested occurrence rank (1-indexed: k=1 means the first time the target code appeared).Print q integers separated by single spaces on one line. For each query k_i, print the scan position (0-indexed) at which the k_i-th occurrence of x in codes occurred, or -1 if x occurs fewer than k_i times in codes.
Example 1
Input
7 4 1 3 4 2 4 4 4 2 1 3
Expected
2 5
Explanation
The target code 4 appears at scan positions 2, 4, 5, and 6. The 1st occurrence (k=1) is at position 2, and the 3rd occurrence (k=3) is at position 5, so the output is "2 5".
Example 2
Input
4 9 9 9 9 9 3 1 4 5
Expected
0 3 -1
Explanation
The target code 9 appears at every position: 0, 1, 2, 3. The 1st occurrence is at 0, the 4th occurrence is at 3, and since there is no 5th occurrence the answer for k=5 is -1. Output: "0 3 -1".
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →