Given an array of integers arr[], reverse the array in place.
Examples:
Input: arr[] = [1, 4, 3, 2, 6, 5]
Output: [5, 6, 2, 3, 4, 1]
Explanation: The elements are rearranged in reverse order, so the first element becomes the last, the second becomes the 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] 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.
#include <iostream>
#include <vector>
using namespace std;
void reverseArray(vector<int> &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<int> arr = {1, 4, 3, 2, 6, 5};
reverseArray(arr);
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
#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;
}
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] + " ");
}
}
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=" ")
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] + " ");
}
}
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(" "));
Output
5 6 2 3 4 1
[Expected Approach - 1] Two Pointers - O(n) Time and O(1) Space
The idea is to reverse the array in place using two pointers, swapping elements from opposite ends and moving towards the center.
- Initialize left = 0 and right = n - 1.
- While left < right, swap arr[left] and arr[right].
- Increment left and decrement right after each swap.
#include <iostream>
#include <vector>
using namespace std;
void reverseArray(vector<int> &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<int> arr = {1, 4, 3, 2, 6, 5};
reverseArray(arr);
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
#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;
}
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] + " ");
}
}
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=" ")
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] + " ");
}
}
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(" "));
Output
5 6 2 3 4 1
[Expected Approach - 2] Single Pointer - O(n) Time and O(1) Space
The idea is to traverse the first half of the array and swap each element arr[i] with its corresponding element from the end, arr[n - i - 1]. This way we swap arr[0] with arr[n-1], arr[1] with arr[n-2] and so on.
#include <iostream>
#include <vector>
using namespace std;
void reverseArray(vector<int> &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<int> arr = {1, 4, 3, 2, 6, 5};
reverseArray(arr);
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
#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;
}
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] + " ");
}
}
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=" ")
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] + " ");
}
}
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(" "));
Output
5 6 2 3 4 1
[Alternate Approach] Inbuilt Methods - O(n) Time and O(n) Space
The idea is to use inbuilt reverse methods available across different languages.
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// function to reverse an array
void reverseArray(vector<int> &arr)
{
reverse(arr.begin(), arr.end());
}
int main()
{
vector<int> arr = {1, 4, 3, 2, 6, 5};
reverseArray(arr);
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
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(int[] arr)
{
List<Integer> list = new ArrayList<>();
for (int x : arr)
list.add(x);
Collections.reverse(list);
for (int i = 0; i < arr.length; i++)
arr[i] = list.get(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] + " ");
}
}
# 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)))
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] + " ");
}
}
// 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(" "));
Output
5 6 2 3 4 1