easyArray ProblemsJava

Reverse an Array Using Recursion

Reverse an array using recursion by swapping the outer pair and recursing on the inner range.

Quick Answer

Swap the element at the left index with the element at the right index, then recurse with left + 1 and right - 1. The base case is when left is no longer less than right, meaning the pointers have met in the middle. Each recursive call reverses one more pair until the whole array is reversed.

Problem Statement

Reverse an array using recursion rather than an explicit loop. The recursive idea mirrors the two-pointer swap: exchange the outermost pair of elements, then recurse on the range that lies strictly inside them.

Modify the array in place. The recursion stops once the left index meets or passes the right index.

Input: An array arr, plus left and right indices (initially 0 and n - 1).

Output: The same array with its elements reversed.

Examples

Example 1
Input:  [1, 2, 3, 4]
Output: [4, 3, 2, 1]

Swap 1 and 4, then swap 2 and 3, then left crosses right and recursion stops.

Example 2
Input:  [7, 8, 9]
Output: [9, 8, 7]

Swap 7 and 9; the middle 8 stays; left meets right so recursion stops.

Constraints

  • 0 <= n <= 10^4 (recursion depth grows with n/2)
  • Reverse in place — do not allocate a second array

Think Before You Code

Reveal the questions to ask yourself first
  • What is the smallest sub-problem that needs no more work (the base case)?
  • After swapping the ends, which smaller range should you recurse on?
  • How do the indices change on each recursive call?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
Each recursive call should reverse exactly one outer pair, then hand off the rest.
Hint 2
The base case is `left >= right` — the pointers have met, so return without swapping.
Hint 3
Swap `arr[left]` and `arr[right]`, then call the method again with `left + 1` and `right - 1`.

Approach

Reveal the step-by-step approach

Reverse the array by turning the two-pointer loop into recursion.

  1. Base case: if left >= right, return — nothing left to swap.
  2. Swap arr[left] and arr[right].
  3. Recurse with reverse(arr, left + 1, right - 1).

Each call peels off the outermost pair and delegates the inner range to the next call, so after about n/2 calls the whole array is reversed.

Dry Run

Walk through the example step by step

Reversing [1, 2, 3, 4] starting with left=0, right=3:

call            | swap    | array          | next
----------------+---------+----------------+------------------
reverse(a,0,3)  | 1 <-> 4 | [4, 2, 3, 1]   | reverse(a,1,2)
reverse(a,1,2)  | 2 <-> 3 | [4, 3, 2, 1]   | reverse(a,2,1)
reverse(a,2,1)  | left>=right (2>=1)       | base case, return
------------------------------- result [4, 3, 2, 1]

Solution

Reveal the full Java solution
import java.util.Arrays;

public class ReverseArrayRecursion {
    public static void reverse(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        int temp = arr[left];
        arr[left] = arr[right];
        arr[right] = temp;
        reverse(arr, left + 1, right - 1);
    }

    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4};
        reverse(a, 0, a.length - 1);
        System.out.println(Arrays.toString(a)); // [4, 3, 2, 1]

        int[] b = {7, 8, 9};
        reverse(b, 0, b.length - 1);
        System.out.println(Arrays.toString(b)); // [9, 8, 7]
    }
}

Recursion replaces the loop's index updates with new arguments on each call. The left >= right base case handles both even lengths (pointers cross) and odd lengths (pointers meet on the untouched middle element), so the method terminates correctly after roughly n/2 swaps.

Time: O(n)Space: O(n) — the recursion stack holds up to n/2 frames

Common Mistakes

  • Using `left > right` as the base case, which double-swaps the middle pair on even lengths and undoes the reversal.
  • Forgetting to advance both indices, causing infinite recursion or a stack overflow.
  • Overwriting `arr[left]` before saving it, losing the value being swapped.

Edge Cases to Test

  • Empty array [] — the very first call hits the base case immediately.
  • Single element [5] — left equals right, so nothing is swapped.
  • Large arrays where recursion depth (n/2) could risk a stack overflow.

Interview Follow-Ups

  • How would you convert this recursion into an iterative two-pointer loop?
  • What is the maximum recursion depth for an array of length n?
  • How would you reverse only a sub-range using the same recursive idea?

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 →
Chat with us