An environmental sensor writes one reading to a log every hour, in strict chronological order. A field engineer suspects the sensor sometimes gets stuck and repeats the same reading for a while. Given the log as an ordered sequence of readings, find every distinct value that appears in an unbroken run of three or more consecutive log entries (a "sustained streak"). If the same value forms more than one separate sustained streak elsewhere in the log, it should still be reported only once.
The first line contains one integer n, the number of log entries. The second line contains n space-separated integers, the readings in chronological order.
Print the qualifying values in ascending order, separated by single spaces, on one line. If no value forms a sustained streak, print an empty line.
Example 1
Input
6 1 1 1 2 1 1
Expected
1
Explanation
The readings 1, 1, 1 form a streak of length 3 starting at the first entry, so 1 qualifies. The trailing 1, 1 only has length 2 and the single 2 has length 1, so nothing else qualifies. Output: 1.
Example 2
Input
9 5 5 5 1 1 2 2 2 9
Expected
2 5
Explanation
5, 5, 5 is a streak of length 3 (qualifies), 1, 1 has length 2 (does not), 2, 2, 2 is a streak of length 3 (qualifies), and the trailing 9 is a single entry. Sorted ascending, the qualifying values are 2 and 5.
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 →