Introduction

Reversing an array means rearranging the elements so that the first element becomes the last, the second element 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

Approach 1 : Brute Force (Using a temporary 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 Array        temp[] = [0, 0, 0, 0, 0, 0]Step 2: Copy in Reverse Order

iarr[n-i-1]temp[]
05[5, 0, 0, 0, 0, 0]
16[5, 6, 0, 0, 0, 0]
22[5, 6, 2, 0, 0, 0]
33[5, 6, 2, 3, 0, 0]
44[5, 6, 2, 3, 4, 0]
51[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 → copy elements into temporary array  Second loop → copy back into original arraySo total operations are proportional to nSpace Complexity:-0(n):Explanation :We An extra temporary array of size n is used.


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

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 element is swapped only once. The loop runs approximately n/2 times.Space Complexity:-O(1):Explanation : No extra array is used. The reversal happens in-place using only two variables.

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

  • Incorrect pointer updates : Forgetting to increment left or decrement right.
  • Forgetting stopping condition  Using:  while left <= right Instead of while left < right
  • Swapping wrong indices : Confusing indices while reversing.
  • Using extra space unnecessarily in optimal solution :Creating another array unnecessarily.

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.

Related Questions

  • Reverse String
  • Valid Palindrome
  • Move Zeroes
  • Container With Most Water
  • Two Sum II