A remote weather beacon streams back a log of integer readings it collected throughout the day, listed in the exact order they were recorded. Given the full log, report the most recently recorded reading -- that is, the last value in the log. If the beacon sent back no readings at all that day, report -1 instead, since there is no last reading to give.
The first line contains a single integer n (0 <= n <= 1000), the number of readings in the log. The second line contains n space-separated integers r_1, r_2, ..., r_n (-10^9 <= r_i <= 10^9), the readings in the order they were recorded. When n = 0 this line is empty (it may also be absent entirely).
Print a single integer: r_n (the last reading in the log) if n > 0, or -1 if n = 0.
0 <= n <= 1000 -10^9 <= r_i <= 10^9
Example 1
Input
5 3 -2 17 8 42
Expected
42
Explanation
The log has 5 readings recorded in order 3, -2, 17, 8, 42. The most recently recorded reading is the last one listed, 42, so that is printed.
Example 2
Input
0
Expected
-1
Explanation
n = 0, so the beacon's log is empty -- there is no last reading to report, so the sentinel -1 is printed instead.
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 →