Reverse an Array In Place

1. What Does “Reverse an Array In Place” Mean?

Reverse an array in place is one of the most fundamental operations in data structures
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.

flowchart figure has descriptive alt text with reverse an Array In Place

4. Pseudocode — Reverse Array In Place

📄 Pseudocode
START
READ n
READ array
left = 0
right = n 1
WHILE left < right:
        SWAP array[left], array[right]
        left = left + 1
        right = right 1
PRINT array
END

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

1

Read Input

n = int(input()) reads the array size.
arr = list(map(int, input().split())) reads all integers into a list.

2

Initialize Two Pointers

left, right = 0, len(arr) - 1left points to the
first element, right points to the last. This is the heart of the
in-place reversal strategy.

3

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.

4

Move Pointers Inward

left += 1 moves forward. right -= 1 moves backward.
After ⌊n/2⌋ swaps, the entire array has been reversed in place.

5

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:

 Time Complexity
O(n)
 Space Complexity
O(1)

9. Test Cases

Case 1
Odd-length array

Input
5
1 2 3 4 5
Output
5 4 3 2 1

Case 2
Negative integers

Input
5
-1 -2 -3 -4 -5
Output
-5 -4 -3 -2 -1

Case 3
Even-length array

Input
4
10 20 30 40
Output
40 30 20 10

Case 4
Single element

Input
1
42
Output
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, a makes the swap clean and readable.
  • Works correctly for odd-length, even-length, negative integers, and single-element arrays.

 

Leave a Comment