A seismograph technician reviews a single day's log of aftershock readings. Each reading is a nonzero integer: a positive value k means the ground rose by k units at that instant, while a negative value -k means it sank by k units. No magnitude is ever logged twice with the same sign, so every reading in the log is distinct.
A magnitude k (k > 0) is called a matched swing if the log contains both the uplift reading k and the subsidence reading -k. The technician wants the single greatest magnitude among all matched swings recorded that day.
Print a single integer: the greatest magnitude k such that both k and -k appear in the log. If no magnitude is matched, print -1.
Example 1
Input
6 -2 5 1 -3 3 -5
Expected
5
Explanation
Readings are -2, 5, 1, -3, 3, -5. Magnitude 5 is matched because both 5 and -5 appear; magnitude 3 is also matched (3 and -3 appear); magnitudes 1 and 2 are not matched since their opposite-sign counterpart is missing. The greatest matched magnitude is 5.
Example 2
Input
4 -4 2 3 -2
Expected
2
Explanation
Readings are -4, 2, 3, -2. Magnitude 2 is matched because both 2 and -2 appear. Magnitude 3 has no -3 counterpart and magnitude 4 has no positive 4 counterpart (only -4 was logged), so 2 is the greatest matched magnitude.
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 →