Array Reverse (original) (raw)
Reverse an array **arr[]. 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] Using Single Pointer - O(n) Time and O(1) 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:
C++ `
#include #include using namespace std;
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
#include <stdio.h> #include <stdlib.h>
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
import java.util.Arrays;
class GfG {
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
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#
using System;
class GfG {
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
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];}
// Driver Code 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:
C++ `
#include #include using namespace std;
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
#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
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
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 -= 1if name == "main": arr = [1, 4, 3, 2, 6, 5]
reverseArray(arr)
for i in range(len(arr)):
print(arr[i], end=" ")C#
using System;
class GfG {
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
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--;
}}
//Driver Code const arr = [1, 4, 3, 2, 6, 5]; reverseArray(arr); console.log(arr.join(" "));
`
[Expected Approach - 2] Using Single Pointer - 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:
C++ `
#include #include using namespace std;
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
#include <stdio.h>
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
import java.util.Arrays;
class GfG {
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
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] = tempif name == "main": arr = [1, 4, 3, 2, 6, 5]
reverseArray(arr)
for i in range(len(arr)):
print(arr[i], end=" ")C#
using System;
class GfG {
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
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;
}}
// Driver Code 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.
**Using Inbuilt Methods - O(n) Time and O(1) Space
The idea is to use inbuilt **reverse methods available across different languages.
C++ `
#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
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List;
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
function to reverse an array
def reverseArray(arr): arr.reverse()
if name == "main": arr = [1, 4, 3, 2, 6, 5]
reverseArray(arr)
print(" ".join(map(str, arr))) C#
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
// function to reverse an array function reverseArray(arr) { arr.reverse(); }
// Driver code 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.