85. Reverse an Array With Two Pointers
Core1000 ms256 MBSolved by 0%
Read n and then n integers, and print them reversed, separated by
single spaces.
The exercise is to use two pointers, one starting at the front and one at the back, moving toward each other and swapping as they go, rather than building a second array.
Constraints - `1 ≤ n ≤ 100000` - `-1000000 ≤ a[i] ≤ 1000000`
Input
The first line contains an integer n.
The second line contains n space-separated integers.
Output
Print the reversed array on ONE line, separated by single spaces.
Input5
1 2 3 4 5
Output5 4 3 2 1
Notehas an odd length, so the middle element stays put
Input4
1 2 3 4
Output4 3 2 1
Notehas an even length, so every element moves
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.
Hint 1Where to start
Hint 2The approachOpen hint 1 first. This one carries on from it.
Hint 3PseudocodeOpen hint 2 first. This one carries on from it.
Hint 4Full solutionOpen hint 3 first. This one carries on from it.