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 UnlockApproach 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.
Dry Run
Input: arr[] = [1, 4, 3, 2, 6, 5]Step 1: Create Temporary Arraytemp[] = [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:
left: Starts at the first index of the array.
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 beginning2. 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 = 5Step 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
How to Explain This in an Interview
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.