Arrays · Easy
2. Reverse the Array
Topics: Array
Solve it on DeVert Campus - free code runner
Problem
Given an array of n integers, reverse it in place and print the reversed array.
Input and output
Input: The first line contains an integer n. The second line contains n space-separated integers.
Output: Print the reversed array as n space-separated integers on one line.
Example
Input: 5 1 2 3 4 5 Output: 5 4 3 2 1
Constraints
1 <= n <= 10^5
Key observation
For an array with an ODD number of elements, the exact middle element never needs to move at all - the two pointers simply meet there and stop.
Intuition
Flip an array so the last element becomes first, the second-last becomes second, and so on.
Brute force: Build a brand new array, copying elements in from back to front - correct, but uses O(n) extra space when the reversal can be done directly within the original array instead.
Optimised: Two pointers moving toward each other from both ends, swapping as they go, reverses the array IN PLACE with no extra array needed at all - each pair of elements only needs to be touched once.
Brute Force - Auxiliary Array
Allocate a new array of the same size and copy elements from the original array starting at the last index down to the first, filling the new array front to back. This trivially produces reversed order because you are literally reading the source backwards. It works correctly but wastes an entire extra array's worth of memory when the problem only asks to reverse the array.
Time: O(n)
Space: O(n)
Optimal - Two-Pointer In-Place Swap
Keep two indices, one starting at the front and one at the back, and repeatedly swap the elements they point to while moving the front pointer forward and the back pointer backward, stopping when they meet or cross. This reverses the array using only a constant number of extra variables and no recursion, making it both time- and space-optimal, and is the standard technique for in-place array reversal.
Time: O(n)
Space: O(1)
Better - Recursive Reversal
Reverse the array using recursion: swap the first and last elements, then recursively reverse the sub-array strictly between them, stopping once the sub-array has zero or one elements. This reverses the array using only the original array's storage (no second array is allocated), which is an improvement over the brute-force copy, but it consumes call stack space proportional to n/2 recursive calls. It's a useful stepping stone that demonstrates in-place swapping before removing the recursion overhead entirely.
Time: O(n)
Space: O(n)
Step by step
1. Point one finger at the very first element, another at the very last.
2. Swap the elements these two fingers are pointing at.
3. Move the first finger one step right, the second finger one step left.
4. Repeat until the two fingers meet or cross in the middle.
Complexity
Time: O(n) - roughly n/2 swaps, one pass through half the array.
Space: O(1) - reversal happens directly within the original array, no extra storage.
Edge cases
Test n = 1 (array unchanged) and n = 10^5 for performance.
Interview tip
The two-pointer-from-both-ends technique shown here is a foundational building block reused throughout many other array and string problems (like checking palindromes) - master it here first.