A team is racing through an escape room full of numbered levers. A wall clock silently logs every lever pull as the pair (lever id, elapsed seconds since the room started), and the log is given to you already sorted by elapsed time in strictly increasing order.
The "pull duration" credited to a logged pull is the time elapsed since the previous pull anywhere in the room (or, for the very first logged pull, the time elapsed since the room started at second 0). Formally, if the log entries in order are (id_1, t_1), (id_2, t_2), ..., (id_n, t_n), the pull duration for entry i is t_i - t_{i-1} (using t_0 = 0).
Determine the id of the lever that is credited with the single longest pull duration anywhere in the log. If two or more pulls tie for the longest duration, report the smallest lever id among those tied pulls.
The first line contains an integer n, the number of logged pulls. Each of the next n lines (or, equivalently, the next 2n whitespace-separated integers) contains two integers id_i and t_i -- the lever pulled and the elapsed time of that pull.
Print a single integer: the lever id credited with the longest pull duration (smallest id among ties).
Example 1
Input
3 1 3 2 7 3 8
Expected
2
Explanation
Durations: lever 1 at t=3 has duration 3-0=3; lever 2 at t=7 has duration 7-3=4; lever 3 at t=8 has duration 8-7=1. The longest duration is 4, credited to lever 2, so the answer is 2.
Example 2
Input
4 2 1 1 2 3 3 2 4
Expected
1
Explanation
Durations: lever 2 at t=1 has duration 1-0=1; lever 1 at t=2 has duration 2-1=1; lever 3 at t=3 has duration 3-2=1; lever 2 at t=4 has duration 4-3=1. All four pulls tie at duration 1, so the answer is the smallest lever id among them, which is 1.
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 →