array inversion
in-place algorithm
constant extra space
data structures
algorithms

Is it possible to invert an array with constant extra space?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Yes, reversing an array with constant extra space is absolutely possible. The standard solution is an in-place two-pointer swap, which uses only a few variables regardless of array size and therefore has O(1) auxiliary space complexity.

The In-Place Idea

To reverse an array, you do not need a second array. You only need to swap matching elements from the ends toward the middle:

  • first with last
  • second with second-last
  • third with third-last

Continue until the pointers meet or cross.

For an array of length n, element i swaps with element n - 1 - i.

A Simple Python Implementation

python
1def reverse_in_place(values):
2    left = 0
3    right = len(values) - 1
4
5    while left < right:
6        values[left], values[right] = values[right], values[left]
7        left += 1
8        right -= 1
9
10
11data = [1, 2, 3, 4, 5]
12reverse_in_place(data)
13print(data)

Output:

text
[5, 4, 3, 2, 1]

The extra space stays constant because only left, right, and the temporary swap state are needed.

The Same Idea in C++

cpp
1#include <iostream>
2#include <vector>
3
4void reverse_in_place(std::vector<int>& values) {
5    std::size_t left = 0;
6    std::size_t right = values.size();
7
8    if (right == 0) {
9        return;
10    }
11
12    --right;
13
14    while (left < right) {
15        std::swap(values[left], values[right]);
16        ++left;
17        --right;
18    }
19}
20
21int main() {
22    std::vector<int> values{1, 2, 3, 4, 5, 6};
23    reverse_in_place(values);
24
25    for (int value : values) {
26        std::cout << value << ' ';
27    }
28}

This also runs in O(n) time because each element is moved at most once.

Why the Space Is Constant

Constant extra space does not mean “no extra bytes at all.” It means the amount of additional memory does not grow with input size.

This algorithm uses a fixed number of variables:

  • one pointer or index for the left side
  • one pointer or index for the right side
  • one temporary location during a swap, depending on the language

Whether the array has 5 elements or 5 million, the algorithm still needs only that small, fixed amount of extra storage.

Odd and Even Length Arrays

The algorithm works for both:

  • Even length: every element participates in one swap.
  • Odd length: the middle element stays where it is.

For example, [1, 2, 3, 4, 5] becomes [5, 4, 3, 2, 1], and the value 3 never needs to move because it is already in the correct middle position after the surrounding swaps.

When You Cannot Do It In Place

The answer changes only if the data structure is immutable. For example, some languages use immutable array-like structures or strings, which means any “reverse” operation must create a new value. In that case, the algorithmic idea is still valid, but the specific data type prevents in-place mutation.

For a normal mutable array, though, constant-space reversal is the standard solution.

Common Pitfalls

The most common mistake is accidentally allocating a second array and calling the result “constant space.” If the extra structure grows with n, the algorithm is no longer O(1) space.

Another issue is off-by-one errors in the right pointer. The last valid index is len(values) - 1, not len(values).

Developers also sometimes continue the loop while left <= right. That still works in some languages, but it causes an unnecessary self-swap of the middle element for odd lengths.

Finally, be careful with unsigned indexes in languages such as C++. If the array is empty, subtracting from zero without a guard can underflow.

Summary

  • Yes, arrays can be reversed with constant extra space.
  • The standard method swaps from both ends toward the center.
  • The algorithm uses O(n) time and O(1) extra space.
  • It works naturally for both odd and even lengths.
  • Watch out for off-by-one errors and empty-array edge cases.

Course illustration
Course illustration

All Rights Reserved.