1. What Does “Reverse an Array In Place” Mean?
and algorithms. When we say “in place,” we mean the reversal must happen inside the same array
without allocating a new one — making the space complexity O(1).The classic approach to reverse an array in place is the two-pointer technique:
place one pointer at the start and one at the end, swap their elements, then move both pointers inward
until they meet. No extra memory is consumed at all.
AllowedModify the original array using index swaps.
Not AllowedCreating a second / extra array to store elements.
2. Problem Statement — Reverse Array In Place
Given an array of integers, reverse the array in place without using extra memory.
You must modify the original array; no extra array is allowed.
Input Format
First line: Integer n (size of array)
Second line: n space-separated integers
Output Format
Print the reversed array as space-separated integers on a single line.
3. Flowchart — Reverse Array In Place Algorithm
The diagram below visually shows how the reverse array in place algorithm works
step by step using the two-pointer method.

4. Pseudocode — Reverse Array In Place
5. Python Code — Reverse Array In Place
Below is the clean Python implementation to reverse an array in place
using the two-pointer technique. The keyword density for “reverse array in place” is
maintained throughout this article for good SEO performance.
n = int(input())
arr = list(map(int, input().split()))
left, right = 0, len(arr) - 1
while left < right:
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1
print(*arr)
6. Step-by-Step Explanation
Read Input
n = int(input()) reads the array size.
arr = list(map(int, input().split())) reads all integers into a list.
Initialize Two Pointers
left, right = 0, len(arr) - 1 — left points to the
first element, right points to the last. This is the heart of the
in-place reversal strategy.
Swap While Pointers Haven’t Crossed
The while left < right loop runs as long as there are unswapped
pairs. arr[left], arr[right] = arr[right], arr[left] swaps them using
Python’s simultaneous assignment — no temporary variable needed.
Move Pointers Inward
left += 1 moves forward. right -= 1 moves backward.
After ⌊n/2⌋ swaps, the entire array has been reversed in place.
Print Result
print(*arr) uses Python’s unpacking operator *
to print all elements space-separated — e.g. 5 4 3 2 1 instead of
[5, 4, 3, 2, 1].
7. Example
Let’s trace through [10, 20, 30, 40] to see exactly how
reverse array in place works at each step:
| Step | left | right | Array State | Action |
|---|---|---|---|---|
| Init | 0 | 3 | [10, 20, 30, 40] | Setup pointers |
| 1 | 0 | 3 | [40, 20, 30, 10] | Swap 10 ↔ 40 |
| 2 | 1 | 2 | [40, 30, 20, 10] | Swap 20 ↔ 30 |
| 3 | 2 | 1 | [40, 30, 20, 10] | left ≥ right → STOP |
8. Time & Space Complexity
The reverse array in place algorithm using two pointers is extremely efficient:
9. Test Cases
Case 1
Odd-length array
1 2 3 4 5
Case 2
Negative integers
-1 -2 -3 -4 -5
Case 3
Even-length array
10 20 30 40
Case 4
Single element
42
10. External Resources & References
Want to practise more problems on reverse array in place and similar
two-pointer techniques? Check out these trusted external resources:
LeetCode — Reverse String (Two-Pointer, In-Place)
Practice
GeeksForGeeks — Reverse an Array or String
Reference
Python Official Docs — Mutable Sequence Types
Docs
Key Takeaways
- To reverse an array in place, use the two-pointer technique — no extra array needed.
- Time complexity is O(n) — only ⌊n/2⌋ swaps are performed.
- Space complexity is O(1) — only two pointer variables are used.
- Python’s simultaneous assignment
a, b = b, amakes the swap clean and readable. - Works correctly for odd-length, even-length, negative integers, and single-element arrays.