# Arrays Questions: Reverse an array

In this series of posts, I will discuss coding questions on the Arrays Data structure.
The posts in this series will be organized in the following way,

Possible Explanation ?
Documented C++ Code ?
Time and Space Complexity Analysis ⌛?…

This content originally appeared on DEV Community and was authored by Kathan Vakharia

In this series of posts, I will discuss coding questions on the `Arrays` Data structure.
The posts in this series will be organized in the following way,

2. Possible Explanation ?
3. Documented C++ Code ?
4. Time and Space Complexity Analysis ⌛?

## The Question

https://www.hackerearth.com/practice/data-structures/arrays/1-d/practice-problems/algorithm/print-array-in-reverse/

? Give yourself at least 15-20 mins to figure out the solution :)

## Explanation

The idea is to maintain two index variables `start` and `end`, initially pointing to the first element and last element of the array respectively.

And then, we will swap values in the following order: (first, last)(second, second-last)(third, third, third-last) ....until we reach the middle element of the array.

Here's the pseudo-code,

``````    while start < end:
swap(arr[start], arr[end])
start = start + 1 //move start ahead by one step
end = end - 1 //move end back by one step
``````

? If you are wondering why there's `<` instead of `≤` ? It's because `start` will be equal to `end` only in the case of odd length arrays and they both will point to the middle element of the array. And it does no good to swap it with itself as the array is already reversed by then.

### Still confused?

Assume index starts from zero.

• Think what happens when `arr.length = 3`(odd), after one iteration, `start` and `end` both will point to index=1 and array is already reversed.

• Think what happens when `arr.length = 4`(even), after two iteration, `start(2)` will be greater than `end(1)` and array will be reversed.

## C++ Code

### Solution

``````#include<iostream>

using namespace std;

void reverse(int* arr, int start, int end){
//untill we reach the middle
while(start < end){
//swap arr[start] and arr[end]
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;

end--;//move end back
}
}

//driver code
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++){
cin>>arr[i];
}

reverse(arr, 0, n-1);

for(auto val: arr){
cout<<val<<"\n";
}

return 0;
}
``````

## Complexity Analysis

`N`: length of the array

### Time Complexity: O(N)

Since we are iterating nearly N/2 times, thus time will be O(N/2) = O(N).

### Space Complexity: O(1)

We didn't use any extra space.

This content originally appeared on DEV Community and was authored by Kathan Vakharia

Print Share Comment Cite Upload Translate
APA
Kathan Vakharia | Sciencx (2023-03-26T05:34:49+00:00) » Arrays Questions: Reverse an array. Retrieved from https://www.scien.cx/2021/07/22/arrays-questions-reverse-an-array/.
MLA
" » Arrays Questions: Reverse an array." Kathan Vakharia | Sciencx - Thursday July 22, 2021, https://www.scien.cx/2021/07/22/arrays-questions-reverse-an-array/
HARVARD
Kathan Vakharia | Sciencx Thursday July 22, 2021 » Arrays Questions: Reverse an array., viewed 2023-03-26T05:34:49+00:00,<https://www.scien.cx/2021/07/22/arrays-questions-reverse-an-array/>
VANCOUVER
Kathan Vakharia | Sciencx - » Arrays Questions: Reverse an array. [Internet]. [Accessed 2023-03-26T05:34:49+00:00]. Available from: https://www.scien.cx/2021/07/22/arrays-questions-reverse-an-array/
CHICAGO
" » Arrays Questions: Reverse an array." Kathan Vakharia | Sciencx - Accessed 2023-03-26T05:34:49+00:00. https://www.scien.cx/2021/07/22/arrays-questions-reverse-an-array/
IEEE
" » Arrays Questions: Reverse an array." Kathan Vakharia | Sciencx [Online]. Available: https://www.scien.cx/2021/07/22/arrays-questions-reverse-an-array/. [Accessed: 2023-03-26T05:34:49+00:00]
rf:citation
» Arrays Questions: Reverse an array | Kathan Vakharia | Sciencx | https://www.scien.cx/2021/07/22/arrays-questions-reverse-an-array/ | 2023-03-26T05:34:49+00:00