C++ Program to Reverse elements of an array

Reverse elements of an Array in C++

 

Here, in this page we will discuss the program to reverse elements of an Array in C++ Programming language. We are given with an array and need to reverse that array. WE will discuss various approaches to reverse the array.

Reverse elements of an array in C++

Method Discussed :

  • Method 1: Printing array in reverse order.
  • Method 2: In-place reversing of the original array
  • Method 3: Recursive in-place reversing of the original array
  • Method 4 : Using inbuilt function

 

Method 1 (Simple Printing Array in Reverse):

Objective: Reverse array in C++

  • Run a loop in reverse order i.e, from index n-1 to index 0.
  • Print i-th element

 

Time and Space Complexity :

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

Method 1 : Code in C++

Run
#include<bits/stdc++.h>
using namespace std;

int main(){

   int arr[] = {10, 20, 30, 40, 50};
   int n = sizeof(arr)/sizeof(arr[0]);

   for(int i=n-1; i>=0; i--)
      cout<<arr[i]<<" ";
}

Output

50 40 30 20 10

Method 2 (In-Place Reversal):

  • Take two variables say i = 0 and j = n – 1.
  • Run a loop till i < j
  • swap(arr[i],  arr[j])
  • Increment i and decrement the j
  • Now, Print the array

Time and Space Complexity :

  • Time Complexity : O(n)
  • Space Complexity : O(1)
Reverse elements of an array in C++ Example 2 new

Method 2 : Code in C++

Run
#include<bits/stdc++.h>
using namespace std;

int main(){

   int arr[] = {10, 20, 30, 40, 50};
   int n = sizeof(arr)/sizeof(arr[0]);

   int start = 0, end = n-1;

    while(start < end){
        swap(arr[start++], arr[end--]);
    }

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

Output

50 40 30 20 10

Method 3 (Recursive):

  • Take variables, start = 0 and end = len – 1
  • Function gets called as reverseRecursive(arr, 0, len-1);
  • In each recursive iteration swap arr[start] & arr[end]
  • Make further recursive calls as reverseRecursive(arr, start + 1, end – 1);
  • return when (start >= end)
  • Print the array

Time and Space Complexity :

  • Time – Complexity : O(n)
  • Space-Complexity : O(n), require stack to store the recursive calls.
Reverse elements of an array in C++

Method 3 : Code in C++

Run
#include<bits/stdc++.h>
using namespace std;

void reverseRecursive(int arr[], int start, int end)
{
   if (start >= end)
      return;

   int temp = arr[start];
   arr[start] = arr[end];
   arr[end] = temp;

   // recursive call to swap arr[start+1] & arr[end-1]
   reverseRecursive(arr, start + 1, end - 1);
}

int main(){

  int arr[] = {10, 20, 30, 40, 50};
  int n = sizeof(arr)/sizeof(arr[0]);

  reverseRecursive(arr, 0, n-1);

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

Output

50 40 30 20 10

Method 4 :

In this method we will use inbuilt reverse function to reverse the array.

  • reverse() is a predefined function in header file algorithm. 
  • It reverses the order of the elements in the range [first, last) of any container.

Time and Space Complexity :

  • Time – Complexity : O(n)
  • Space-Complexity : O(1)
Reversing array in C++

Method 4 : Code in C++

Run
#include<bits/stdc++.h>
using namespace std;

int main(){

   int arr[] = {10, 20, 30, 40, 50};
   int n = sizeof(arr)/sizeof(arr[0]);

   reverse(arr, arr+n);

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

Output

50 40 30 20 10

Important Codes related to Arrays

  • Find Smallest Element in an Array : C | C++ | Java | Python
  • Find Second Smallest Element in an Array : C | C++ | Java | Python
  • Find Largest element in an array : C | C++ | Java | Python
  • Find the Smallest and largest element in an array : C | C++ | Java | Python
  • Calculate the sum of elements in an array : C | C++ | Java | Python
  • Reverse an Array : C | C++ | Java | Python
  • Sort first half in ascending order and second half in descending : C | C++ | Java | Python
  • Sort the elements of an array : C | C++ | Java | Python
  • Finding the frequency of elements in an array : C | C++ | Java | Python
  • Sorting elements of an array by frequency : C | C++ | Java | Python
  • Finding the Longest Palindrome in an Array : C | C++ | JavaPython
  • Counting Distinct Elements in an Array : C | C++ | JavaPython
  • Finding  Repeating elements in an Array : C++ | Java | Python
  • Finding Non Repeating elements in an Array : C | C++ | Java | Python
  • Removing Duplicate elements from an array : C | C++ | Java | Python
  • Finding Minimum scalar product of two vectors : C | C++ | Java | Python
  • Finding Maximum scalar product of two vectors in an array : C | C++ | Java | Python
  • Counting the number of even and odd elements in an array : C | C++ | Java | Python
  • Find all Symmetric pairs in an array : C | C++ | Java | Python
  • Find maximum product sub-array in a given array : C | C++ | Java | Python
  • Finding Arrays are disjoint or not : C | C++ | Java | Python
  • Determine Array is a subset of another array or not : C | C++ | Java | Python
  • Determine can all numbers of an array be made equal : C | C++ | Java | Python
  • Finding Minimum sum of absolute difference of given array : C | C++ | Java | Python
  • Sort an array according to the order defined by another array : C | C++ | Java | Python
  • Replace each element of the array by its rank in the array : C | C++ | Java | Python
  • Finding equilibrium index of an array : C | C++ | JavaPython
  • Rotation of elements of array- left and right : C | C++ | JavaPython
  • Block swap algorithm for array rotation : C | C++ | JavaPython
  • Juggling algorithm for array rotation : C | C++ | Java | Python
  • Finding Circular rotation of an array by K positions : C | C++ | Java | Python
  • Balanced Parenthesis Problem : C | C++ | Java | Python

4 comments on “C++ Program to Reverse elements of an array”


  • luckyjainseth

    #include
    using namespace std;
    int main()
    {
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i>arr[i];

    }
    cout<<endl;
    int s=0;
    int e=n-1;
    while(s<e)
    {
    swap(arr[s],arr[e]);
    s++;
    e–;

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


  • Raghvendra Singh

    #include
    using namespace std;
    int rev(int* a,int N)
    {
    int i,j=N-1,temp;
    for(i=0;i<j;i++,j–)
    {
    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
    }
    cout<<"reverse array: ";
    for(i=0;i<N;i++)
    cout<<" "<<a[i];
    return 1;
    }
    int main()
    {
    int a[20],n,i;
    cout<>n;
    cout<<"Enter elements: ";
    for(i=0;i>a[i];
    rev(a,n);
    return 0;
    }


  • vamsichowdary

    #include
    using namespace std;
    int main(){
    int n,temp=0;
    cin >> n;
    int a[n-1];
    for(int i=0;i> a[i];
    }
    int l=n;
    for(int i=1;i<=n/2;i++){
    temp=a[i-1];
    a[i-1]=a[l-1];
    a[l-1]=temp;
    l–;
    }
    for(int i=0;i<n;i++){
    cout << a[i];
    }
    }


  • Y.PRAVEEN

    #include
    using namespace std;

    int main()
    {
    int n;
    cin >> n;
    int arr[n];
    int i;
    for(i = 0; i > arr[i];
    }
    cout <= 0; i–)
    {
    cout << arr[i] <<" ";
    }
    cout << endl;
    return 0;

    }