A conveyor belt carries a singly linked list of package codes, front to back. Find the MIDDLE package. If the belt holds an even number of packages (so there are two middle packages), report the one that is FARTHER from the front. Print that package's code.
Line 1: an integer n — the number of packages.
Line 2: n space-separated integers — the package codes, front to back.
A single integer: the code of the middle package as defined above.
Example 1
Input
5 1 2 3 4 5
Expected
3
Explanation
n is odd, so the single middle (index 2, 0-based) is 3.
Example 2
Input
4 1 2 3 4
Expected
3
Explanation
n is even; the two middles are 2 and 3, and 3 is farther from the front, so the answer is 3.
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 →