You are given an array of integers arr[]. You have to reverse the given array.…

202420252025

You are given an array of integers arr[]. You have to reverse the given array.

Note: Modify the array in place — reverse it without building a second array.

Input format:
The first line contains a single integer n — the number of elements in arr[].
The second line contains n space-separated integers arr[0], arr[1], …, arr[n-1].

Output format:
Print the reversed array as n space-separated integers on a single line.

Examples:

Input:
6
1 4 3 2 6 5
Output:
5 6 2 3 4 1
Explanation: The elements of the array are [1, 4, 3, 2, 6, 5]. After reversing the array, the first element goes to the last position, the second element goes to the second last position and so on. Hence, the answer is [5, 6, 2, 3, 4, 1].

Input:
3
4 5 2
Output:
2 5 4
Explanation: The elements of the array are [4, 5, 2]. The reversed array will be [2, 5, 4].

Input:
1
1
Output:
1
Explanation: The array has only a single element, hence the reversed array is the same as the original.

Expected time complexity: O(n). Expected auxiliary space: O(1).

Constraints:
1 ≤ n ≤ 105
0 ≤ arr[i] ≤ 105

Attempted by 1 students.

Show answer & explanation

Concept

Reversing a sequence in place is an index-mirroring operation: whatever sits at index i must end up at index n − 1 − i, and whatever sits at n − 1 − i must end up at i. Because those two destinations are each other, one swap of the pair (i, n − 1 − i) puts both elements in their final positions at the same time.

So a left pointer starting at 0 and a right pointer starting at n − 1, swapping and stepping toward each other until they meet, finish the whole array in ⌊n/2⌋ swaps. Every element’s destination depends only on its own index and n, and each swap is self-contained, which is exactly why no second array is ever needed — the reversal is done in O(1) auxiliary space.

Applying it to this problem

  1. Read n from the first line, then read the n values of arr[] from the second line.

  2. Set i = 0 (left pointer) and j = n − 1 (right pointer).

  3. While i < j: swap arr[i] with arr[j], then do i = i + 1 and j = j − 1. The strict i < j is what stops the pointers at the midpoint instead of crossing over and undoing the work.

  4. Print arr[] as n space-separated integers on one line.

Trace for arr = [1, 4, 3, 2, 6, 5] (n = 6):

i

j

array after this step

0

5

[5, 4, 3, 2, 6, 1]

1

4

[5, 6, 3, 2, 4, 1]

2

3

[5, 6, 2, 3, 4, 1]

3

2

i < j is false — the loop stops, array is [5, 6, 2, 3, 4, 1]

Three swaps for six elements, which is the predicted ⌊6/2⌋ = 3.

Cross-check

  • Odd length, arr = [4, 5, 2] (n = 3): i = 0, j = 2 swap gives [2, 5, 4]; now i = 1 and j = 1, so i < j is false and the loop stops. The middle index is its own mirror (n − 1 − i = i when n = 3 and i = 1), so leaving it untouched is exactly right.

  • Single element, arr = [1] (n = 1): i = 0 and j = 0, the loop body never runs, and a one-element array is already its own reverse.

  • Cost: each of the ⌊n/2⌋ iterations does constant work, so the time is O(n) and the auxiliary space is O(1). With n up to 105, that is well inside the 2-second limit, while an approach that repeatedly inserts at the front of a new array would cost O(n²).

  • Common slip: running i over the full range 0 … n − 1 and swapping arr[i] with arr[n − 1 − i] every time reverses the array and then reverses it back, returning the original order. The loop must stop at the midpoint.

Reference implementation (C++)

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    if (!(cin >> n)) return 0;

    vector<int> arr(n);
    for (int i = 0; i < n; ++i) cin >> arr[i];

    // Two pointers walking toward each other; each swap places BOTH endpoints.
    for (int i = 0, j = n - 1; i < j; ++i, --j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    for (int i = 0; i < n; ++i) {
        cout << arr[i];
        if (i + 1 < n) cout << ' ';
    }
    cout << '\n';
    return 0;
}

Explore the full course: Coding For Placement

Loading lesson…