A lab technician exports a log of n integer calibration readings from a sensor, sorted in non-decreasing order. It is known that exactly one reading value occurs strictly more than 25% of the time (that is, its count is strictly greater than n / 4), while every other distinct value occurs at most n / 4 times. Because the log is huge, the technician wants a program that identifies this dominant reading quickly by taking advantage of the fact that the log is already sorted.
Given the sorted log, report the dominant reading value.
Example 1
Input
9 1 2 2 6 6 6 6 7 10
Expected
6
Explanation
The value 6 appears 4 times out of 9 readings, and 4 > 9/4 = 2.25, while every other value (1, 2, 7, 10) appears at most 2 times, which does not exceed 2.25. So 6 is the dominant reading.
Example 2
Input
6 1 1 1 2 3 4
Expected
1
Explanation
The value 1 appears 3 times out of 6 readings, and 3 > 6/4 = 1.5, while 2, 3, and 4 each appear only once, which does not exceed 1.5. So 1 is the dominant reading.
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 →