Reverse an Array

Practice this coding question on Coding Gurukul.

Problem statement

A museum stores exhibit numbers in the order used for its morning tour. The afternoon tour walks the same exhibits in the opposite direction, so the stored route must be reversed. Your task Reverse the array in place and print its elements in the new order. Important details Do not sort the numbers: only their positions change. Repeated and negative identifiers are allowed. Swap matching positions from the two ends and move inward, using O(n) time and O(1) auxiliary space. With an odd number of entries, the middle entry stays in place. Worked example The route 4, 9, 2, 7 becomes 7, 2, 9, 4. A route containing only 5 remains 5.

Input format

The first line contains n. The second line contains n integers.

Output format

Print the reversed array, separated by spaces.

Constraints

1 <= n <= 100000; -1000000000 <= a[i] <= 1000000000.

Sample input

4
4 9 2 7

Sample output

7 2 9 4

Sign in to run or submit a solution in C++, Java or Python.

Loading Coding Gurukul…