Array Reverse Complete Tutorial (original) (raw)
Last Updated : 25 Sep, 2024
Given an array **arr[], the task is to **reverse the array. Reversing an array means **rearranging the elements such that the **first element becomes the **last, the **second element becomes **second last and so on.
**Examples:
**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.
Table of Content
- [Naive Approach] Using a temporary array – O(n) Time and O(n) Space
- [Expected Approach – 1] Using Two Pointers – O(n) Time and O(1) Space
- [Expected Approach – 2] By Swapping Elements – O(n) Time and O(1) Space
- [Alternate Approach] Using Recursion – O(n) Time and O(n) Space
- Using Inbuilt Methods – O(n) Time and O(1) Space
**[Naive Approach] Using a temporary array – O(n) Time and O(n) Space
The idea is to use a **temporary array to store the reverse of the array.
- Create a **temporary array of same size as the original array.
- Now, copy all elements from original array to the temporary array in **reverse order.
- Finally, copy all the elements from temporary array back to the original array.
**Working:
Below is the implementation of the algorithm:
C++ `
// C++ Program to reverse an array using temporary array
#include #include using namespace std;
// function to reverse an array void reverseArray(vector &arr) { int n = arr.size();
// Temporary array to store elements in reversed order
vector<int> temp(n);
// Copy elements from original array to temp in reverse order
for(int i = 0; i < n; i++)
temp[i] = arr[n - i - 1];
// Copy elements back to original array
for(int i = 0; i < n; i++)
arr[i] = temp[i];
}
int main() { vector arr = { 1, 4, 3, 2, 6, 5 };
reverseArray(arr);
for(int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
C
// C Program to reverse an array using temporary array
#include <stdio.h> #include <stdlib.h>
// function to reverse an array void reverseArray(int arr[], int n) {
// Temporary array to store elements in reversed order
int temp[n];
// Copy elements from original array to temp in reverse order
for(int i = 0; i < n; i++)
temp[i] = arr[n - i - 1];
// Copy elements back to original array
for(int i = 0; i < n; i++)
arr[i] = temp[i];
}
int main() { int arr[] = { 1, 4, 3, 2, 6, 5 }; int n = sizeof(arr) / sizeof(arr[0]);
reverseArray(arr, n);
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Java
// Java Program to reverse an array using temporary array
import java.util.Arrays;
class GfG {
// function to reverse an array
static void reverseArray(int[] arr) {
int n = arr.length;
// Temporary array to store elements in reversed order
int[] temp = new int[n];
// Copy elements from original array to temp in reverse order
for (int i = 0; i < n; i++)
temp[i] = arr[n - i - 1];
// Copy elements back to original array
for (int i = 0; i < n; i++)
arr[i] = temp[i];
}
public static void main(String[] args) {
int[] arr = { 1, 4, 3, 2, 6, 5 };
reverseArray(arr);
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
}
}
Python
Python Program to reverse an array using temporary array
function to reverse an array
def reverseArray(arr): n = len(arr)
# Temporary array to store elements in reversed order
temp = [0] * n
# Copy elements from original array to temp in reverse order
for i in range(n):
temp[i] = arr[n - i - 1]
# Copy elements back to original array
for i in range(n):
arr[i] = temp[i]
if name == "main": arr = [1, 4, 3, 2, 6, 5]
reverseArray(arr)
for i in range(len(arr)):
print(arr[i], end=" ")
C#
// C# Program to reverse an array using temporary array
using System;
class GfG {
// function to reverse an array
static void reverseArray(int[] arr) {
int n = arr.Length;
// Temporary array to store elements in reversed order
int[] temp = new int[n];
// Copy elements from original array to temp in reverse order
for (int i = 0; i < n; i++)
temp[i] = arr[n - i - 1];
// Copy elements back to original array
for (int i = 0; i < n; i++)
arr[i] = temp[i];
}
static void Main() {
int[] arr = { 1, 4, 3, 2, 6, 5 };
reverseArray(arr);
for (int i = 0; i < arr.Length; i++)
Console.Write(arr[i] + " ");
}
}
JavaScript
// JavaScript Program to reverse an array using temporary array
// function to reverse an array function reverseArray(arr) { let n = arr.length;
// Temporary array to store elements in reversed order
let temp = new Array(n);
// Copy elements from original array to temp in reverse order
for (let i = 0; i < n; i++)
temp[i] = arr[n - i - 1];
// Copy elements back to original array
for (let i = 0; i < n; i++)
arr[i] = temp[i];
}
const arr = [1, 4, 3, 2, 6, 5];
reverseArray(arr);
console.log(arr.join(" "));
`
**Time Complexity: O(n), Copying elements to a new array is a linear operation.
**Auxiliary Space: O(n), as we are using an extra array to store the reversed array.
[Expected Approach – 1] Using Two Pointers – O(n) Time and O(1) Space
The idea is to maintain two pointers: **left and **right, such that **left points at the **beginning of the array and **right points to the **end of the array.
While left pointer is less than the right pointer, swap the elements at these two positions. After each swap, **increment the **left pointer and **decrement the **right pointer to move towards the center of array. This will swap all the elements in the first half with their corresponding element in the second half.
**Working:
Below is the implementation of the algorithm:
C++ `
// C++ Program to reverse an array using Two Pointers
#include #include using namespace std;
// function to reverse an array void reverseArray(vector &arr) {
// Initialize left to the beginning and right to the end
int left = 0, right = arr.size() - 1;
// Iterate till left is less than right
while(left < right) {
// Swap the elements at left and right position
swap(arr[left], arr[right]);
// Increment the left pointer
left++;
// Decrement the right pointer
right--;
}
}
int main() { vector arr = { 1, 4, 3, 2, 6, 5 };
reverseArray(arr);
for(int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
C
// C Program to reverse an array using Two Pointers
#include <stdio.h>
// Function to swap two numbers void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; }
// function to reverse an array void reverseArray(int arr[], int n) {
// Initialize left to the beginning and right to the end
int left = 0, right = n - 1;
// Iterate till left is less than right
while (left < right) {
// Swap the elements at left and right position
swap(&arr[left], &arr[right]);
// Increment the left pointer
left++;
// Decrement the right pointer
right--;
}
}
int main() { int arr[] = { 1, 4, 3, 2, 6, 5 }; int n = sizeof(arr) / sizeof(arr[0]);
reverseArray(arr, n);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Java
// Java Program to reverse an array using Two Pointers
import java.util.Arrays;
class GfG {
// function to reverse an array
static void reverseArray(int[] arr) {
// Initialize left to the beginning and right to the end
int left = 0, right = arr.length - 1;
// Iterate till left is less than right
while (left < right) {
// Swap the elements at left and right position
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
// Increment the left pointer
left++;
// Decrement the right pointer
right--;
}
}
public static void main(String[] args) {
int[] arr = { 1, 4, 3, 2, 6, 5 };
reverseArray(arr);
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
}
}
Python
Python Program to reverse an array using Two Pointers
function to reverse an array
def reverseArray(arr):
# Initialize left to the beginning and right to the end
left = 0
right = len(arr) - 1
# Iterate till left is less than right
while left < right:
# Swap the elements at left and right position
arr[left], arr[right] = arr[right], arr[left]
# Increment the left pointer
left += 1
# Decrement the right pointer
right -= 1
if name == "main": arr = [1, 4, 3, 2, 6, 5]
reverseArray(arr)
for i in range(len(arr)):
print(arr[i], end=" ")
C#
// C# Program to reverse an array using Two Pointers
using System;
class GfG {
// function to reverse an array
static void reverseArray(int[] arr) {
// Initialize left to the beginning and right to the end
int left = 0, right = arr.Length - 1;
// Iterate till left is less than right
while (left < right) {
// Swap the elements at left and right position
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
// Increment the left pointer
left++;
// Decrement the right pointer
right--;
}
}
static void Main() {
int[] arr = { 1, 4, 3, 2, 6, 5 };
reverseArray(arr);
for (int i = 0; i < arr.Length; i++)
Console.Write(arr[i] + " ");
}
}
JavaScript
// JavaScript Program to reverse an array using Two Pointers
// function to reverse an array function reverseArray(arr) {
// Initialize left to the beginning and right to the end
let left = 0, right = arr.length - 1;
// Iterate till left is less than right
while (left < right) {
// Swap the elements at left and right position
[arr[left], arr[right]] = [arr[right], arr[left]];
// Increment the left pointer
left++;
// Decrement the right pointer
right--;
}
}
const arr = [1, 4, 3, 2, 6, 5];
reverseArray(arr);
console.log(arr.join(" "));
`
**Time Complexity: O(n), as we are visiting each element exactly once.
**Auxiliary Space: O(1)
[Expected Approach – 2] By Swapping Elements – O(n) Time and O(1) Space
The idea is to iterate over the **first half of the array and **swap each element with its corresponding element from the **end. So, while iterating over the first half, any element at index **i is swapped with the element at index ****(n – i – 1)**.
**Working:
Below is the implementation of the algorithm:
C++ `
// C++ Program to reverse an array by swapping elements
#include #include using namespace std;
// function to reverse an array void reverseArray(vector &arr) { int n = arr.size();
// Iterate over the first half and for every index i,
// swap arr[i] with arr[n - i - 1]
for(int i = 0; i < n/2; i++) {
swap(arr[i], arr[n - i - 1]);
}
}
int main() { vector arr = { 1, 4, 3, 2, 6, 5 };
reverseArray(arr);
for(int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
C
// C Program to reverse an array by swapping elements
#include <stdio.h>
// function to reverse an array void reverseArray(int arr[], int n) {
// Iterate over the first half and for every index i,
// swap arr[i] with arr[n - i - 1]
for (int i = 0; i < n / 2; i++) {
int temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}
}
int main() { int arr[] = { 1, 4, 3, 2, 6, 5 }; int n = sizeof(arr) / sizeof(arr[0]);
reverseArray(arr, n);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Java
// Java Program to reverse an array by swapping elements
import java.util.Arrays;
class GfG {
// function to reverse an array
static void reverseArray(int[] arr) {
int n = arr.length;
// Iterate over the first half and for every index i,
// swap arr[i] with arr[n - i - 1]
for (int i = 0; i < n / 2; i++) {
int temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}
}
public static void main(String[] args) {
int[] arr = { 1, 4, 3, 2, 6, 5 };
reverseArray(arr);
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
}
}
Python
Python Program to reverse an array by swapping elements
def reverseArray(arr): n = len(arr)
# Iterate over the first half and for every index i,
# swap arr[i] with arr[n - i - 1]
for i in range(n // 2):
temp = arr[i]
arr[i] = arr[n - i - 1]
arr[n - i - 1] = temp
if name == "main": arr = [1, 4, 3, 2, 6, 5]
reverseArray(arr)
for i in range(len(arr)):
print(arr[i], end=" ")
C#
// C# Program to reverse an array by swapping elements
using System;
class GfG {
// function to reverse an array
static void reverseArray(int[] arr) {
int n = arr.Length;
// Iterate over the first half and for every index i,
// swap arr[i] with arr[n - i - 1]
for (int i = 0; i < n / 2; i++) {
int temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}
}
static void Main() {
int[] arr = { 1, 4, 3, 2, 6, 5 };
reverseArray(arr);
for (int i = 0; i < arr.Length; i++)
Console.Write(arr[i] + " ");
}
}
JavaScript
// JavaScript Program to reverse an array by swapping elements
// function to reverse an array function reverseArray(arr) { let n = arr.length;
// Iterate over the first half and for every index i,
// swap arr[i] with arr[n - i - 1]
for (let i = 0; i < n / 2; i++) {
let temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}
}
const arr = [1, 4, 3, 2, 6, 5];
reverseArray(arr); console.log(arr.join(" "));
`
**Time Complexity: O(n), the loop runs through half of the array, so it’s linear with respect to the array size.
**Auxiliary Space: O(1), no extra space is required, therefore we are reversing the array **in-place.
**[Alternate Approach] Using Recursion – O(n) Time and O(n) Space
The idea is to use recursion and define a **recursive function that takes a range of array elements as input and reverses it. Inside the recursive function,
- Swap the first and last element.
- Recursively call the function with the remaining subarray.
C++ `
// C++ Program to reverse an array using Recursion
#include #include using namespace std;
// recursive function to reverse an array from l to r void reverseArrayRec(vector &arr, int l, int r) { if(l >= r) return;
// Swap the elements at the ends
swap(arr[l], arr[r]);
// Recur for the remaining array
reverseArrayRec(arr, l + 1, r - 1);
}
// function to reverse an array void reverseArray(vector &arr) { int n = arr.size(); reverseArrayRec(arr, 0, n - 1); }
int main() { vector arr = { 1, 4, 3, 2, 6, 5 };
reverseArray(arr);
for(int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
C
// C Program to reverse an array using Recursion
#include <stdio.h>
// recursive function to reverse an array from l to r void reverseArrayRec(int arr[], int l, int r) { if(l >= r) return;
// Swap the elements at the ends
int temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
// Recur for the remaining array
reverseArrayRec(arr, l + 1, r - 1);
}
// function to reverse an array void reverseArray(int arr[], int n) { reverseArrayRec(arr, 0, n - 1); }
int main() { int arr[] = { 1, 4, 3, 2, 6, 5 }; int n = sizeof(arr) / sizeof(arr[0]);
reverseArray(arr, n);
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Java
// Java Program to reverse an array using Recursion
import java.util.Arrays;
class GfG {
// recursive function to reverse an array from l to r
static void reverseArrayRec(int[] arr, int l, int r) {
if (l >= r)
return;
// Swap the elements at the ends
int temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
// Recur for the remaining array
reverseArrayRec(arr, l + 1, r - 1);
}
// function to reverse an array
static void reverseArray(int[] arr) {
int n = arr.length;
reverseArrayRec(arr, 0, n - 1);
}
public static void main(String[] args) {
int[] arr = { 1, 4, 3, 2, 6, 5 };
reverseArray(arr);
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
}
}
Python
Python Program to reverse an array using Recursion
recursive function to reverse an array from l to r
def reverseArrayRec(arr, l, r): if l >= r: return
# Swap the elements at the ends
arr[l], arr[r] = arr[r], arr[l]
# Recur for the remaining array
reverseArrayRec(arr, l + 1, r - 1)
function to reverse an array
def reverseArray(arr): n = len(arr) reverseArrayRec(arr, 0, n - 1)
if name == "main": arr = [1, 4, 3, 2, 6, 5]
reverseArray(arr)
for i in range(len(arr)):
print(arr[i], end=" ")
C#
// C# Program to reverse an array using Recursion
using System;
class GfG {
// recursive function to reverse an array from l to r
static void reverseArrayRec(int[] arr, int l, int r) {
if (l >= r)
return;
// Swap the elements at the ends
int temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
// Recur for the remaining array
reverseArrayRec(arr, l + 1, r - 1);
}
// function to reverse an array
static void reverseArray(int[] arr) {
int n = arr.Length;
reverseArrayRec(arr, 0, n - 1);
}
static void Main(string[] args) {
int[] arr = { 1, 4, 3, 2, 6, 5 };
reverseArray(arr);
for (int i = 0; i < arr.Length; i++)
Console.Write(arr[i] + " ");
}
}
JavaScript
// JavaScript Program to reverse an array using Recursion
// recursive function to reverse an array from l to r function reverseArrayRec(arr, l, r) { if (l >= r) return;
// Swap the elements at the ends
[arr[l], arr[r]] = [arr[r], arr[l]];
// Recur for the remaining array
reverseArrayRec(arr, l + 1, r - 1);
}
// function to reverse an array function reverseArray(arr) { let n = arr.length; reverseArrayRec(arr, 0, n - 1); }
let arr = [1, 4, 3, 2, 6, 5];
reverseArray(arr);
console.log(arr.join(" "));
`
**Time Complexity: O(n), the recurrence relation will be **T(n) = T(n – 2) + O(1), which can be simplified to O(n).
**Auxiliary Space: O(n), as we are using recursion stack.
**Using Inbuilt Methods – O(n) Time and O(1) Space
The idea is to use inbuilt **reverse methods available across different languages.
C++ `
// C++ Program to reverse an array using inbuilt methods
#include #include #include using namespace std;
// function to reverse an array void reverseArray(vector &arr) { reverse(arr.begin(), arr.end()); }
int main() { vector arr = { 1, 4, 3, 2, 6, 5 };
reverseArray(arr);
for(int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
Java
// Java Program to reverse an array using inbuilt methods
import java.util.*;
class GfG {
// function to reverse an array
static void reverseArray(List<Integer> arr) {
Collections.reverse(arr);
}
public static void main(String[] args) {
List<Integer> arr =
new ArrayList<>(Arrays.asList(1, 4, 3, 2, 6, 5));
reverseArray(arr);
for (int i = 0; i < arr.size(); i++)
System.out.print(arr.get(i) + " ");
}
}
Python
Python Program to reverse an array using inbuilt methods
function to reverse an array
def reverse_array(arr): arr.reverse()
if name == "main": arr = [1, 4, 3, 2, 6, 5]
reverse_array(arr)
print(" ".join(map(str, arr)))
C#
// C# Program to reverse an array using inbuilt methods
using System;
class GfG {
// function to reverse an array
static void reverseArray(int[] arr) {
Array.Reverse(arr);
}
static void Main() {
int[] arr = { 1, 4, 3, 2, 6, 5 };
reverseArray(arr);
for (int i = 0; i < arr.Length; i++)
Console.Write(arr[i] + " ");
}
}
JavaScript
// JavaScript Program to reverse an array using inbuilt methods
// function to reverse an array function reverseArray(arr) { arr.reverse(); }
const arr = [1, 4, 3, 2, 6, 5];
reverseArray(arr);
console.log(arr.join(" "));
`
**Time Complexity: O(n), the reverse method has linear time complexity.
**Auxiliary Space: O(1) Additional space is not used to store the reversed array, as the in-built array method swaps the values in-place.