A factory floor sensor logs a continuous stream of integer readings into an array. Safety engineers care about what happens immediately after a specific alarm code is logged: for every position in the stream where the alarm code appears, other than the very last position (which has nothing after it), the reading recorded right after it is called a response reading. Determine the response reading value that occurs most often across all triggers of the alarm code. If two or more response values are tied for the highest frequency, report the smallest of those tied values.
The first line contains a single integer n, the number of logged readings. The second line contains n space-separated integers, readings[0] through readings[n-1]. The third line contains a single integer, alarmCode.
A single integer: the most frequent response reading (the smallest such value if there is a tie).
Example 1
Input
7 3 50 75 50 50 50 75 50
Expected
50
Explanation
The alarm code 50 appears at indices 1, 3, 4, and 5 (all strictly before the last index, 6). The readings right after those triggers are 75, 50, 50, and 75 respectively, so 75 occurs twice and 50 occurs twice -- a tie. The smaller of the tied values, 50, is reported.
Example 2
Input
5 9 9 9 9 9 9
Expected
9
Explanation
The alarm code 9 appears at indices 0, 1, 2, and 3 (index 4 is the last index and is excluded). Each trigger is followed by another 9, so the response reading 9 occurs 4 times, the only candidate, and is reported.
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 →