Command Palette

Search for a command to run...

Problem 0.1 · Java for DSAEasy

Reverse an Array In Place

What it teaches: Your first two-pointer algorithm: swap from both ends towards the middle, using O(1) extra space.

The problem

Given an integer array nums, reverse it in place: change the array itself so its elements appear in the opposite order. Don't create a second array.

Example 1

Input: nums = [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]

Example 2

Input: nums = [7, 8]
Output: [8, 7]

Example 3

Input: nums = [42]
Output: [42]

A single element is already reversed.

Constraints

  • 0 ≤ nums.length ≤ 10⁵
  • −10⁹ ≤ nums[i] ≤ 10⁹
  • O(1) extra space

Pattern clues in the wording

  • → "In place" and "don't create a second array" rule out copying
  • → The first and last elements trade places, then the second and second-to-last: a symmetric job from both ends

These clues point to Two Pointers: Opposite Ends: Start one pointer at each end and move them towards each other, using a rule to decide which one moves.

Stuck? Take one hint at a time

Solution.java · starter
class Solution {
    public void reverse(int[] nums) {
        // swap from both ends towards the middle
    }
}

Write your solution locally or in your editor for now. The in-browser runner (Java first, then Python, C++ and more) will run these tests right here.

Test cases

#InputExpected
1
nums = [1,2,3,4,5]
[5,4,3,2,1]
2
nums = [7,8]
[8,7]
3
nums = [42]
[42]
4
nums = []
Empty input
[]

+ 1 hidden test the code runner will check

From slow to fast

Approaches

1

Brute force: copy into a new array

Time O(n) Space O(n)

Make a second array of the same length, fill it from the back of the original, then copy it back. Easy to get right, but it uses O(n) extra memory, which the problem forbids.

Approach 1
class Solution {
    public void reverse(int[] nums) {
        int n = nums.length;
        int[] copy = new int[n];
        for (int i = 0; i < n; i++) copy[i] = nums[n - 1 - i];
        for (int i = 0; i < n; i++) nums[i] = copy[i];
    }
}

Verdict: Correct, but breaks the O(1) space rule. Good as a first step to check your understanding.

2

Optimal: two pointers swapping inwards

Time O(n) Space O(1)

Put left at the start and right at the end. Swap the two elements, then move left one step right and right one step left. Every swap fixes two positions for good.

Stop when left >= right: if they meet at the same index (odd length), that middle element stays where it is.

  1. Set left = 0, right = n - 1.
  2. While left < right: swap nums[left] and nums[right], then left++, right--.
▶ Dry run: Reversing five numbersnums = [1, 2, 3, 4, 5]
1
0
↑L
2
1
3
2
4
3
5
4
↑R

Step 1/5Start with L at the first element and R at the last.

Approach 2
class Solution {
    public void reverse(int[] nums) {
        int left = 0, right = nums.length - 1;
        while (left < right) {
            int tmp = nums[left];
            nums[left] = nums[right];
            nums[right] = tmp;
            left++;
            right--;
        }
    }
}

Verdict: Each element is touched once and no extra array is needed. This is the answer.

Before you submit

Edge cases and common mistakes

Test these inputs

  • Empty array (right starts at −1, the loop never runs)
  • One element
  • Two elements
  • Odd and even lengths

Mistakes people make

  • Looping i from 0 to n - 1 and swapping each i with n - 1 - i: every pair is swapped twice, so the array ends up unchanged. Stop at the middle.
  • Using left <= right: harmless here (swapping the middle with itself), but a habit that causes bugs in other two-pointer problems.
  • Losing a value by writing nums[left] = nums[right] before saving nums[left] in a temporary variable.

Interview

Follow-up questions

How would you reverse only the part of the array between indexes i and j?

Can you reverse a String the same way?