Reverse an Array: 4 Methods in C, Python, and Java
Four methods to reverse an array: print-from-end, auxiliary array, in-place swap, and recursion. Working code in C, Python, and Java with complexity analysis.
Reversing an array means reordering its elements so the last element becomes the first and the first becomes the last. Four approaches exist, and they differ only in space complexity.
Given:
Input: arr = {1, 2, 3, 4, 5}
Output: arr = {5, 4, 3, 2, 1}
The standard interview question asks not just for working code but for the approach with the best space profile. That makes the comparison below worth understanding before the coding round.
Array reversal appears across many contexts: displaying search history in reverse chronological order, implementing undo stacks, processing palindrome checks, and traversing graphs in post-order. The algorithm itself is simple; what placement tests care about is which version you reach for first and whether you can articulate the trade-off.
Method 1: Print from the Last Index
The simplest approach. Traverse the array from index n - 1 down to 0 and print each element. The original array is not modified.
// C — print reversed without modifying the array
#include <stdio.h>
void printReversed(int arr[], int n) {
int i;
for (i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
printReversed(arr, n);
return 0;
}
# Python — print reversed
arr = [1, 2, 3, 4, 5]
for i in range(len(arr) - 1, -1, -1):
print(arr[i], end=" ")
Output: 5 4 3 2 1
- Time complexity: O(n)
- Auxiliary space: O(1) — no extra array, only a loop counter
The limitation: this does not actually reverse the array in memory. If the calling code needs a reversed array (not just reversed output), use one of the methods below.
Method 2: Auxiliary Array
Allocate a second array of the same size. Copy elements from the original into the auxiliary array in reverse order, then copy back.
// C — auxiliary array reversal
#include <stdio.h>
void reverseAuxiliary(int arr[], int n) {
int temp[n];
int i;
for (i = 0; i < n; i++) {
temp[i] = arr[n - 1 - i];
}
for (i = 0; i < n; i++) {
arr[i] = temp[i];
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
reverseAuxiliary(arr, n);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
# Python — auxiliary array
arr = [1, 2, 3, 4, 5]
temp = arr[::-1] # slice creates a reversed copy
arr = temp
print(arr)
Output: [5, 4, 3, 2, 1]
According to the Python documentation, the slice operator arr[::-1] returns a new list object; it does not modify the original in place.
- Time complexity: O(n)
- Auxiliary space: O(n) — a second array of n elements
Use this when you need to preserve the original array and work from a reversed copy.
Pointer Variation in C
The same auxiliary-array logic can be written using pointer arithmetic instead of array indexing. A pointer advances through the array from the end toward the beginning on each iteration.
// C — using pointer arithmetic to print reversed
#include <stdio.h>
void reversePointer(int n, int *a) {
int i;
printf("Reversed array: ");
for (i = n - 1; i >= 0; i--) {
printf("%d ", a[i]);
}
printf("\n");
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
reversePointer(n, arr);
return 0;
}
Output: Reversed array: 5 4 3 2 1
Passing the array as a pointer (int *a) and indexing with a[i] is functionally identical to passing int arr[]. C treats both as a pointer to the first element.
Method 3: In-Place Two-Pointer Swap
This is the answer interviewers expect. Two variables, left and right, start at opposite ends of the array. Swap the elements they point to, advance left, retreat right, and repeat until they meet.
// C — in-place swap
#include <stdio.h>
void reverseInPlace(int arr[], int n) {
int left = 0, right = n - 1, temp;
while (left < right) {
temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
reverseInPlace(arr, n);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
# Python — in-place swap
arr = [1, 2, 3, 4, 5]
left, right = 0, len(arr) - 1
while left < right:
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1
print(arr)
// Java — in-place swap
public class ReverseArray {
public static void reverseInPlace(int[] arr) {
int left = 0, 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[] arr = {1, 2, 3, 4, 5};
reverseInPlace(arr);
for (int x : arr) System.out.print(x + " ");
}
}
Output: 5 4 3 2 1
Step-by-Step Trace for arr = 5
- Step 1: left=0, right=4, swap arr[0]=1 with arr[4]=5, result:
{5, 2, 3, 4, 1} - Step 2: left=1, right=3, swap arr[1]=2 with arr[3]=4, result:
{5, 4, 3, 2, 1} - Step 3: left=2, right=2, condition
left < rightis false, loop exits
The middle element (3) is never touched. The array is fully reversed.
- Time complexity: O(n)
- Auxiliary space: O(1) — only the
tempvariable
The C language standard documented at cppreference.com guarantees that array elements occupy contiguous memory, so index-based swaps are safe and predictable for any fixed-size array.
Method 4: Recursion
Swap the first and last elements, then recurse on the sub-array from index 1 to n - 2. The base case is when the sub-array has zero or one element.
# Python — recursive reversal
def reverseRecursive(arr, left, right):
if left >= right:
return
arr[left], arr[right] = arr[right], arr[left]
reverseRecursive(arr, left + 1, right - 1)
arr = [1, 2, 3, 4, 5]
reverseRecursive(arr, 0, len(arr) - 1)
print(arr)
// Java — recursive reversal
public class ReverseArray {
public static void reverseRecursive(int[] arr, int left, int right) {
if (left >= right) return;
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
reverseRecursive(arr, left + 1, right - 1);
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
reverseRecursive(arr, 0, arr.length - 1);
for (int x : arr) System.out.print(x + " ");
}
}
Output: 5 4 3 2 1
- Time complexity: O(n)
- Auxiliary space: O(n/2) = O(n) — the call stack depth is n/2 frames at peak
Complexity Comparison
| Method | Time | Auxiliary Space | Modifies Original? |
|---|---|---|---|
| Print from last index | O(n) | O(1) | No |
| Auxiliary array | O(n) | O(n) | Yes (after copy-back) |
| In-place two-pointer swap | O(n) | O(1) | Yes |
| Recursion | O(n) | O(n) | Yes |
The two methods with O(1) space are the print traversal and the in-place swap. The print traversal does not modify the array; the in-place swap does. In almost every placement coding question, modification of the original array is required, which makes the in-place swap the right answer.
For a deeper look at how auxiliary space and call-stack space count toward an algorithm’s total space cost, the FACE Prep article on space complexity with worked examples breaks down O(1), O(n), and O(log n) classes with the same array-traversal framing.
Two adjacent array problems that appear in the same placement coding round as array reversal: finding symmetric pairs in an array (a hash-map optimisation problem) and the equilibrium index of an array (a prefix-sum optimisation problem). Both reward the same instinct: recognising when O(1) or O(n) space choices change the answer.
Knowing which approach costs what in memory is a skill that separates candidates on online judges where memory limits matter as much as time limits. TinkerLLM is where you can practise that instinct with live AI model feedback on your code, starting at ₹299. The same two-pointer pattern used here shows up across array problems that interviewers cycle through regularly.
Primary sources
Frequently asked questions
What is the time complexity of reversing an array?
All four methods run in O(n) time because each element must be visited at least once. The difference is in space: in-place swap uses O(1) extra space; auxiliary array and recursion use O(n).
What is the difference between in-place and auxiliary-array reversal?
In-place reversal swaps elements within the original array without allocating a second array, so extra space is O(1). The auxiliary-array approach copies elements into a new array in reverse order, requiring O(n) extra space.
How do you reverse an array using pointers in C?
Declare two pointer variables pointing to the first and last elements. Swap the values they point to, advance the left pointer forward, and retreat the right pointer backward. Repeat until the two pointers meet or cross.
Can you reverse an array without using an extra array?
Yes. The in-place two-pointer swap uses only three variables: left index, right index, and a temporary swap variable, regardless of array size. This is the preferred interview answer.
How does recursion reverse an array, and what is the stack depth?
The recursive function swaps the first and last elements, then calls itself on the inner sub-array. The recursion depth is n/2 for an array of n elements, so the call stack uses O(n) space.
Which reversal method do placement interviewers prefer?
Interviewers expect the in-place two-pointer swap as the primary answer because it demonstrates O(1) space awareness. Mentioning the O(n) recursive and auxiliary approaches as trade-offs, and explaining why, rounds out the answer.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹299 launch)