Badges are scanned one at a time, left to right. Maintain a running count of how many times each badge value has been seen so far. Report the 0-indexed position of the scan at which some value's running count first reaches exactly k. If no value ever reaches k occurrences, report -1.
Line 1: two integers n and k.
Line 2: n space-separated integers, the badge values in scan order.
A single integer: the 0-indexed position of the scan at which a running count first equals k, or -1.
Example 1
Input
6 2 3 1 3 2 1 3
Expected
2
Explanation
At index 2 the value 3 has been seen twice, the first time any running count reaches k=2, so the answer is 2.
Example 2
Input
3 3 5 5 1
Expected
-1
Explanation
No value is seen 3 times within these scans, so the answer is -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 →