Reverse an Array In Place
Reverse the elements of an array in place using two pointers and swaps.
Use two indices, one at the start and one at the end. Swap the elements they point to, then move the start index forward and the end index backward. Stop when they meet in the middle. This reverses the array using only a temporary variable, so the extra space is O(1).
Problem Statement
Given an array, reverse the order of its elements in place — that is, without allocating a second array. The first element should end up last, the last should end up first, and so on.
Modify the given array directly and return it (or simply mutate it). The goal is O(1) extra space using a swap-based approach.
Input: An array arr of length n.
Output: The same array with its elements in reversed order.
Examples
Input: [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]The middle element 3 stays put while the outer pairs swap.
Input: [10, 20, 30, 40]
Output: [40, 30, 20, 10]With an even length, every element is part of a swapped pair.
Constraints
0 <= n <= 10^6Extra space must be O(1) — no second array
Think Before You Code
Reveal the questions to ask yourself first
- Which two elements should you swap first?
- When should the two pointers stop moving toward each other?
- Do you need a temporary variable to swap two values safely?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Reverse the array by swapping symmetric pairs from the outside in.
- Set
left = 0andright = n - 1. - While
left < right:- Swap
arr[left]andarr[right]using a temporary variable. - Increment
leftand decrementright.
- Swap
- Stop when the pointers meet (odd length) or cross (even length).
Only pairs are swapped, and each element is touched once, so the array is reversed in a single half-length pass with just one temp variable of extra space.
Dry Run
Walk through the example step by step
Reversing [1, 2, 3, 4, 5]:
left | right | swap | array
-----+-------+-------------+----------------
0 | 4 | 1 <-> 5 | [5, 2, 3, 4, 1]
1 | 3 | 2 <-> 4 | [5, 4, 3, 2, 1]
2 | 2 | left==right | stop
------------------------------ result [5, 4, 3, 2, 1]
Solution
Reveal the full Java solution
import java.util.Arrays;
public class ReverseArrayInPlace {
public static void reverse(int[] arr) {
int left = 0;
int right = arr.length - 1;
while (left < right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 5};
reverse(a);
System.out.println(Arrays.toString(a)); // [5, 4, 3, 2, 1]
int[] b = {10, 20, 30, 40};
reverse(b);
System.out.println(Arrays.toString(b)); // [40, 30, 20, 10]
}
}
The two-pointer swap reverses the array without a second array because each pair of mirror-image positions is exchanged exactly once. The loop runs n/2 times, and the only extra memory is a single temporary variable, giving O(1) space.
O(n)Space: O(1)Common Mistakes
- Looping until `left <= right`, which swaps the middle element with itself harmlessly but, if combined with wrong bounds, can double-swap and undo the reversal.
- Assigning `arr[left] = arr[right]` before saving `arr[left]`, losing the original value.
- Creating a new array and copying in reverse, which uses O(n) extra space instead of O(1).
Edge Cases to Test
- Empty array [] — the loop never runs and the array stays empty.
- Single element [7] — already reversed, no swaps happen.
- Even length arrays where every element is part of a swap pair.
Interview Follow-Ups
- How would you reverse only a sub-range [i, j] of the array?
- Can you reverse the array recursively instead of with a loop?
- How would you rotate the array by k positions using reversals?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →