How to reverse an array in C++?

In this article, we will discuss different ways to reverse an array in C++.

Table Of Contents

Problem Description

Here we are given an array and we have to reverse it. For example,

Input Array

int arr[] = {12, 56, 823, 7, 1023};

Output

1023 7 823 56 12

There are different ways to reverse an array in C++. Let’s see them one by one.

Reverse an Array by creating another array

In this method, we will iterate over the whole array in reverse and fill the values in a new array. This new array will contain the values in reversed order.

These are following steps :

  1. Firstly we check the size of array.
  2. Create a new array of size of given array.
  3. Copy values from original array to new array but from the end to start.
  4. Print the reversed array.

Time Complexity: O(n)
Space Complexity: O(n)

Example

// C++ program to reverse an array
#include <iostream>

using namespace std;

// Driver Code
int main()
{
    int arr[] = {12, 56, 823, 7, 1023};

    // n is the size of array
    int n = sizeof(arr) / sizeof(arr[0]);

    int reversed[n];

    // filling the value in reversed array
    for(int i=0; i<n; i++)
    {
        reversed[i] = arr[n-1 - i];
    }

    // Print the reversed array
    cout<<"Reversed array is"<<endl;

    for(int i=0;i<n;i++)
    {
        cout<<reversed[i]<<" ";
    }
    cout<<endl;

    return 0;
}

Output

Reversed array is
1023 7 823 56 12 

In this example, we created a new array with the reversed contents.

Reverse an Array by swaping the values till half of the array

In this method we reduced the space complexity of above method. By swaping the values in the array itself, to reverse the contents.

These are following steps

  1. Iterate till the half array using loop till (size of array)/2.
  2. Swap the value at ith index with the (n-1-i)th index. Where n is the size of array.
  3. Print the reversed array.

Time Complexity: O(n)
Space Complexity: O(1)

Example

// C++ program to reverse an array

#include <iostream>
#include <algorithm>
using namespace std;

// Driver Code
int main()
{
    int arr[] = {12, 56, 823, 7, 1023};

    // n is the size of array
    int n = sizeof(arr) / sizeof(arr[0]);

    // reversed the value 
    for(int i=0; i<n/2; i++)
    {
        // swap() is STL function which is
        // used for swaping two values
        swap(arr[i], arr[n-1-i]);
    }

    // Print the array
    cout<<"Reversed array is"<<endl;

    for(int i=0; i<n; i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
    return 0;
}

Output

Reversed array is
1023 7 823 56 12 

In this exampl, we reversed the contents of an array in place.

Reverse an Array by using STL function

In this method we use STL function reverse() to reverse the contents of an array in C++.

STL function is : reverse()

template <class BidirectionalIterator>
  void reverse (BidirectionalIterator first, BidirectionalIterator last);

It accepts a range as arguments and reverses the order of the elements in the given range [first,last).

Time Complexity: O(n)
Space Complexity: O(1)

Example

// C++ program to reverse an array

#include <iostream>
#include <algorithm>

using namespace std;

// Driver Code
int main()
{
    int arr[] = {12, 56, 823, 7, 1023};

    // n is the size of array
    int n = sizeof(arr) / sizeof(arr[0]);

    // using STL function for reversing the array 
    reverse(arr, arr+n);

    // Print the value
    cout<<"Reversed array is"<<endl;
    for(int i=0; i<n; i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
    return 0;
}

Output

Reversed array is
1023 7 823 56 12 

In this example, we used a STL function to reverse the contents of an array in place in C++.

Summary

We have seen three different method to reverse an array in C++. One is naive solution where Time complexity and space complexity is O(n). Then in the another methods we reduced the space time complexity and third one is using the STL function. We can see every method has it’s own time complexity and space complexity.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top