A survey rover moves along a straight corridor and logs the ID of every beacon it detects, in the exact order of detection. Over the course of its run, some beacon IDs are picked up far more often than others.
Define the corridor's dominance count as the largest number of times any single beacon ID appears anywhere in the full log. Your task is to find the length of the shortest contiguous block of the log (a run of consecutive detections) whose own most-repeated beacon ID appears exactly that many times — a block that reproduces the corridor's dominance count using as few consecutive detections as possible.
A single integer: the length of the shortest contiguous block whose maximum internal repeat count equals the corridor's overall dominance count.
Example 1
Input
5 1 2 2 3 1
Expected
2
Explanation
Beacon 1 appears at positions 0 and 4 (twice) and beacon 2 appears at positions 1 and 2 (twice) — both tie for the corridor's dominance count of 2. The shortest block that reproduces a repeat count of 2 is the two-element block [2,2] at positions 1-2, so the answer is 2.
Example 2
Input
7 1 2 2 3 1 4 2
Expected
6
Explanation
Beacon 2 appears three times, which is the largest count of any beacon (the dominance count). Its first appearance is at position 1 and its last is at position 6, so the shortest block containing all three of its occurrences spans positions 1 through 6, giving a length of 6.
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 →