A harbor's automated watch station keeps a single patrol log of n entries; entry i records the beacon code broadcast by whichever buoy the station's radar detected at that moment (the same code may appear at several entries). A sweep is any contiguous block of exactly k consecutive log entries — there are n-k+1 such sweeps in total. A beacon code is called solitary if, among all of those sweeps, exactly one of them contains at least one entry carrying that code. Given the log and k, report the largest solitary beacon code, or -1 if no code qualifies.
The first line contains two integers n and k.
The second line contains n integers, the beacon codes in the order they were logged.
A single integer: the largest solitary beacon code, or -1 if none exists.
Example 1
Input
5 3 3 9 2 1 7
Expected
7
Explanation
The 3 sweeps of length 3 are [3,9,2], [9,2,1], [2,1,7]. Code 3 appears only in the first sweep, and code 7 appears only in the third sweep, so both are solitary; the larger is 7.
Example 2
Input
6 4 3 9 7 2 1 7
Expected
3
Explanation
The sweeps of length 4 are [3,9,7,2], [9,7,2,1], [7,2,1,7]. Code 3 (only at the first position) is covered only by the first sweep, so it is solitary. Code 7 appears at positions 2 and 5, and is covered by both the first and the third sweeps, so it is not solitary. The only solitary code is 3.
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 →