Problem Statement

You are given an integer array arr of size n. Reverse its elements so that the first element becomes the last, the second becomes the second-last, and so on

Example :

Input: arr[] = [1, 4, 3, 2, 6, 5]  
Output:  [5, 6, 2, 3, 4, 1]
Explanation: The first element 1 moves to last position, the second element 4 moves to second-last and so on.

Input: arr[] = [4, 5, 1, 2]
Output: [2, 1, 5, 4]
Explanation: The first element 4 moves to last position, the second element 5 moves to second last and so on. ,write for article 

Constraints

 1 <= n <= 10^5-10^9 <= arr[i] <= 10^9

Premium Video

This video is available to Premium members or users who have purchased the course.

Sign in to Unlock

Approach 1 : Brute Force (Using a temporary array)

Intuition

The simplest way to reverse an array is to create a second array and store the original elements in reverse order.

For every index i in the temporary array, copy the element from the corresponding mirror index of the original array, which is n - 1 - i.

After filling the temporary array, copy its elements back into the original array.

Explanations: 

Explanation: The idea is to use a temporary array to store the elements in reverse order.Steps:  1. Create a temporary array temp[] of the same size as the original array. 2. Copy elements from the original array into temp[] in reverse order. 3. Copy all elements from temp[] back into the original array.
This approach is simple and easy to understand but uses extra space.
Flow Diagram
Loading diagram...

Dry Run

 Input: arr[] = [1, 4, 3, 2, 6, 5]Step 1: Create Temporary Array        temp[] = [0, 0, 0, 0, 0, 0]Step 2: Copy in Reverse Order

i

arr[n - 1 - i]

temp[] after copying

0

arr[5] = 5

[5, 0, 0, 0, 0, 0]

1

arr[4] = 6

[5, 6, 0, 0, 0, 0]

2

arr[3] = 2

[5, 6, 2, 0, 0, 0]

3

arr[2] = 3

[5, 6, 2, 3, 0, 0]

4

arr[1] = 4

[5, 6, 2, 3, 4, 0]

5

arr[0] = 1

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

Final Output: [5, 6, 2, 3, 4, 1]

Practice : 

Complexity Analysis : 

Time Complexity:- O(n)Explanation :

We traverse the array twice:

  • First loop: Copies elements into the temporary array.

  • Second loop: Copies elements back into the original array.

Therefore, the total number of operations is proportional to 2n, which simplifies to O(n).

Space Complexity:-0(n):Explanation :We An extra temporary array of size n is used.

Why This Approach Is Not Optimal

Although this approach correctly reverses the array, it requires an additional temporary array of size n.

For an array containing 10⁵ elements, this requires extra memory proportional to the input size.

Since the original array can be reversed directly by swapping elements from both ends, allocating another array is unnecessary.

This limitation leads to the more efficient two-pointer approach, which reverses the array in place using O(1) auxiliary space.

Approach 2 : Optimal Solution(Using Two Pointer -O(n) Time and O(1) Space)

Intuition

To reverse an array without using extra space, we can use the Two Pointer Technique.

The idea is to maintain two pointers:

  1. left: Starts at the first index of the array.

  2. right: Starts at the last index of the array.

Swap the elements at these two positions and move both pointers toward the center.

Continue this process until the pointers meet or cross each other.

Each swap places two elements in their correct reversed positions. Therefore, we only need to process half of the array.

This approach modifies the original array directly without using an additional array.

Explanations: 

 Explanation: This is the most optimized and interview-preferred solution.The idea is to maintain two pointers:  1. left → starts from the beginning  2. right→ starts from the endSwap elements at both positions and move pointers toward the center.

Dry Run

Input: arr[] = [1, 4, 3, 2, 6, 5]Initial State : left = 0 right = 5Step 1Swap: arr[0]and arr[5] Output: [5, 4, 3, 2, 6, 1]Move pointers: left = 1 right = 4Step 2Swap: arr[1]and arr[4] Output: [5, 6, 3, 2, 4, 1]Move pointers: left = 2 right = 3Step 3Swap: arr[2]and arr[3] Output:[5, 6, 2, 3, 4, 1]Move pointers: left = 3 right = 2Now Loop stops because left >= right.

Practice : 

Complexity Analysis : 

Time Complexity:- O(n)Explanation :

Each iteration swaps two elements and moves both pointers toward the center. Therefore, the loop executes approximately n/2 times.Ignoring constant factors, the time complexity is O(n).

Space Complexity:-O(1):Explanation : No extra array is used. The reversal happens in-place using only two variables.

Approach 3: Reverse an Array Using Recursion

Intuition

Recursion can also be used to reverse an array by swapping elements at the beginning and end, then calling the same function for the remaining portion of the array.

The idea is similar to the two-pointer approach, but instead of using a loop, we use recursive function calls.

Steps:

Initialize two pointers: left = 0 and right = n - 1.

If left is greater than or equal to right, stop the recursion.

Swap the elements at arr[left] and arr[right].

Call the function recursively with left + 1 and right - 1.

Continue until the base condition is reached.

Dry Run:

Input: arr[] = [1, 4, 3, 2, 6, 5]Initial State: left = 0, right = 5
Step 1Swap: arr[0] and arr[5]Output: [5, 4, 3, 2, 6, 1]Recursive Call: reverseArray(arr, 1, 4)
Step 2Swap: arr[1] and arr[4]Output: [5, 6, 3, 2, 4, 1]Recursive Call: reverseArray(arr, 2, 3)
Step 3Swap: arr[2] and arr[3]Output: [5, 6, 2, 3, 4, 1]Recursive Call: reverseArray(arr, 3, 2)
Step 4The base condition is satisfied because left is greater than right. Recursion stops.Final Output: [5, 6, 2, 3, 4, 1]

Practice

Complexity Analysis

Time Complexity: O(n)Explanation:Each recursive call swaps one pair of elements and moves both pointers toward the center. Approximately n/2 recursive calls are made, resulting in O(n) time complexity.
Space Complexity: O(n)Explanation:Each recursive call occupies space in the function call stack. Since approximately n/2 recursive calls are made, the auxiliary space complexity is O(n).

Built-in Alternatives

 (Real Code, Not the Interview Answer)

Most programming languages provide built-in functions or methods to reverse arrays or lists.

These methods make array reversal easier in real-world applications. However, understanding the manual two-pointer implementation is important for coding interviews.

C++

The std::reverse() function reverses the elements in the specified range.

Java

Collections.reverse() can reverse a list. For an array, it can be used through Arrays.asList() with an Integer array.

Python

The list.reverse() method reverses a list in place and returns None. The slicing operation [::-1] creates a new reversed list.

JavaScript

The reverse() method reverses an array in place. The toReversed() method creates a new reversed array and leaves the original unchanged.

Practice – Built-in Implementations

Important Notes:

C++: std::reverse() modifies the original range.

Java: Arrays.asList() creates a list backed by the original Integer array. Collections.reverse() therefore changes the array's element order. This does not work as intended with a primitive int[] array.

Python: list.reverse() modifies the original list and returns None. The slicing operation [::-1] creates a new list.

JavaScript: reverse() modifies the original array. toReversed() creates a new array and does not modify the original.

Complexity:

Time Complexity: O(n) for all these methods.Space Complexity: O(1) auxiliary space for the in-place methods. O(n) for methods that create a new reversed array.

Comparison of All Approaches

Approach

Time Complexity

Auxiliary Space

In Place?

When to Use

Temporary Array

O(n)

O(n)

No

When the original array must remain unchanged until the reversed copy is created.

Two Pointers

O(n)

O(1)

Yes

Best choice for interviews and in-place reversal.

Recursion

O(n)

O(n)

Yes

When the problem specifically requires recursion.

Built-in Methods

O(n)

O(1) or O(n)

Depends

Convenient for real-world applications.

Edge Cases

Input

Expected Output

Explanation

[]

[]

An empty array requires no swaps.

[42]

[42]

A single element remains unchanged.

[1, 2]

[2, 1]

Only one swap is required.

[7, 7, 7]

[7, 7, 7]

Swapping equal elements does not change the result.

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

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

Negative values are reversed based on their positions.

Note: The given constraints specify that n is at least 1. The empty-array case is an additional implementation test, not an input allowed by the stated constraints.

Why This Problem is Important

This problem builds the foundation for:

  • Two Pointer problems
  • String reversal
  • Array rotations
  • In-place algorithms
  • Advanced traversal techniques

Real-World Applications

Reversing arrays is used in:

  • Image processing
  • Data transformations
  • Undo operations
  • String manipulation
  • Signal processing systems

Common Beginner Mistakes

1. Forgetting to Move the Pointers
If we forget to increment left or decrement right, the loop continues swapping the same pair of elements indefinitely.
Fix: Always update both pointers after every swap.

2. Looping Through the Entire Array
Swapping every element with its corresponding mirror element from both ends causes each pair to be swapped twice, returning the array to its original order.
Fix: Use the condition left < right or iterate only until the middle of the array.

3. Using an Unsigned Type for the Right Pointer in C++
If the array is empty, calculating arr.size() - 1 using an unsigned type can cause an underflow.
Fix: Use a signed integer for the pointer when handling potentially empty arrays, or explicitly check whether the array is empty first.

4. Expecting Python list.reverse() to Return the Reversed List
The reverse() method modifies the original list and returns None.
Fix: Call arr.reverse() separately. If a new reversed list is required, use arr[::-1].

5. Using Collections.reverse() Directly with a Primitive Array in Java
Arrays.asList() does not convert a primitive int[] into a list of individual integers. It creates a list containing the array itself as one element.
Fix: Use Integer[] with Arrays.asList(), or implement reversal using two pointers.

6. Using left <= right Unnecessarily
The condition left <= right is not incorrect. However, when the pointers meet at the middle element, it performs an unnecessary swap of that element with itself.
Fix: Use left < right to avoid the unnecessary middle-element swap.

How to Explain This in an Interview

"I would solve this problem using the Two Pointer Technique. I will initialize one pointer at the beginning of the array and another at the end.

I will swap the elements at these two positions and move both pointers toward the center. I will continue this process until the pointers meet or cross each other.

Each swap places two elements in their correct reversed positions, so approximately n/2 swaps are required.

The overall time complexity is O(n), and the auxiliary space complexity is O(1), because the reversal happens directly in the original array."

Interview Tip

Interviewers often expect:

  • an in-place solution
  • use of Two Pointer Technique
  • O(1) space complexity

Always explain why the Two Pointer approach is more optimized than using an extra array.

Follow-up Questions

Follow-up Questions the Interviewer May Ask

1. Can you reverse an array using only one index?
Yes. We can iterate from index 0 to n/2 - 1 and swap arr[i] with arr[n - 1 - i]. This achieves the same result using O(n) time and O(1) auxiliary space.

2. Can you swap two elements without using a temporary variable?
Yes. XOR swapping can be used for integer values. However, it is less readable and can cause problems when both references point to the same location. A temporary variable is generally preferred.

3. Can you reverse only a portion of an array?
Yes. Initialize left and right with the starting and ending indices of the required portion and apply the same two-pointer technique.

Example:
Input: [1, 2, 3, 4, 5, 6]
Reverse indices 1 to 4.
Output: [1, 5, 4, 3, 2, 6]

4. How can you rotate an array to the right by k positions?
The reversal algorithm can be extended to array rotation using three reversal operations:
- Reverse the entire array.
- Reverse the first k elements.
- Reverse the remaining elements.
Before performing these operations, handle k using k = k % n and handle an empty array separately.

5. Can the same approach be used to reverse a string?
Yes. We can use two pointers to swap characters from both ends. In languages where strings are immutable, such as Java, Python and JavaScript, convert the string into a mutable character array or another suitable representation before swapping.

Practice Problems

1. Reverse an Array (Easy): Implement array reversal using the two-pointer technique.
2. Reverse a Subarray [l, r] (Easy): Reverse only the elements between the given starting and ending indices.
3. Reverse String (Easy): Apply the same two-pointer technique to reverse a string.
4. Rotate Array (Medium): Use the reversal technique to rotate an array by k positions.

For more coding practice, visit the ExamAdda Practice workspace.

Related Questions

1. Reverse String – LeetCode 344: Apply the same two-pointer technique to reverse characters.
2. Valid Palindrome – LeetCode 125: Use two pointers to compare characters from both ends.
3. Rotate Array – LeetCode 189: Apply the reversal technique to rotate an array.
4. Move Zeroes – LeetCode 283: Use two pointers to rearrange elements.
5. Two Sum II – LeetCode 167: Use two pointers starting from opposite ends of a sorted array.
6. Container With Most Water – LeetCode 11: Use two pointers and make decisions based on the heights at both ends.