← Back to list

Reverse an Array in C++ — Efficient Array Reversal Algorithm

Learn how to reverse an array in C++ using a simple algorithm. This post covers reversing an array with code, edge cases.

Gaurav Sah · 2024-09-20 12:56 · 0 claps · 1.7 min read
#reverse-array #array-reversal-algorithm #gauravsah #vector-reversal #dsa-problem
Open on Medium ↗
Wiki topics: RAG · RAG & Retrieval 💻 · Programming

Problem: Reverse an Array

You are given an array of integers arr . Your task is to reverse the given array and return the reversed array.

You are given an array of integers arr . Your task is to reverse the given array and return the reversed array.

Problem Statement:

Given an array arr, write a function to reverse the elements of the array. The function should return a new array that contains the elements of arr in reverse order.

Example

Example 1:

  • Input: arr = [1, 2, 3, 4, 5]
  • Output: [5, 4, 3, 2, 1]
  • Explanation: The array [1, 2, 3, 4, 5] is reversed to [5, 4, 3, 2, 1].

Example 2:

  • Input: arr = [9, 8, 7]
  • Output: [7, 8, 9]
  • Explanation: The array [9, 8, 7] is reversed to [7, 8, 9].

Approach

  1. Initialization: First, get the size of the input array arr and create a new array revarr of the same size to store the reversed elements.
  2. Reverse Using a Loop: Use a loop to iterate through the input array from the beginning to the end. In each iteration, assign the element from the end of arr to the corresponding position in revarr.
  3. Return the Reversed Array: Once all elements have been copied in reverse order, return the revarr array.

Solution Code (C++)

class Solution {
public:
    vector<int> reverseArray(vector<int> &arr) {
        int size = arr.size();  // Get the size of the array
        vector<int> revarr(size);  // Create a new array of the same size

        // Loop to reverse the array
        for (int i = 0; i < size; i++) {
            revarr[i] = arr[size - 1 - i];  // Assign elements from the end of arr
        }

        return revarr;  // Return the reversed array
    }
};

Explanation

  1. Array Size: The size of the input array is determined using arr.size().
  2. Reversing Elements: The loop starts from index 0 and runs until the end of the array. For each index i, the corresponding element from the end of arr is copied to revarr[i].
  3. Final Output: After the loop completes, the reversed array revarr is returned.

Time and Space Complexity

  • Time Complexity: The time complexity is O(n) where n is the number of elements in the array, because each element is accessed and copied exactly once.
  • Space Complexity: The space complexity is O(n) because we create a new array revarr to store the reversed elements.

Edge Cases

  • Empty Array: If the input array is empty, the function will return an empty array without entering the loop.
  • Single Element Array: If the input array has only one element, the function will return the same array, since reversing a single-element array doesn’t change its order.

메타데이터
post_id
56f014d072ae
slug
reverse-an-array-in-c-efficient-array-reversal-algorithm-56f014d072ae
url
https://medium.com/@gauravssah/reverse-an-array-in-c-efficient-array-reversal-algorithm-56f014d072ae
canonical_url
https://medium.com/@gauravssah/reverse-an-array-in-c-efficient-array-reversal-algorithm-56f014d072ae
author_url
https://medium.com/@gauravssah
status
ok
fetched_at
2026-07-21 03:40:02