Skip to content

Commit 5a8963e

Browse files
committed
Create Reverse an Array Practice GeeksforGeeks.md
1 parent fb4d7a6 commit 5a8963e

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
created: 2025-12-17
3+
modified:
4+
completed: false
5+
platform:
6+
"problem-id":
7+
link: "https://www.geeksforgeeks.org/problems/reverse-an-array/0"
8+
difficulty:
9+
tags:
10+
- "/problem"
11+
---
12+
# Reverse an Array | Practice | GeeksforGeeks
13+
14+
## Question
15+
### Reverse an Array
16+
17+
You are given an array of integers **arr\[\]**. You have to **reverse** the given array.
18+
19+
**Note:**Modify the array in place.
20+
21+
**Examples:
22+
**
23+
24+
```
25+
Input: arr = [1, 4, 3, 2, 6, 5]
26+
Output: [5, 6, 2, 3, 4, 1]
27+
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].
28+
```
29+
```
30+
Input: arr = [4, 5, 2]
31+
Output: [2, 5, 4]
32+
Explanation: The elements of the array are [4, 5, 2]. The reversed array will be [2, 5, 4].
33+
```
34+
```
35+
Input: arr = [1]
36+
Output: [1]
37+
Explanation: The array has only single element, hence the reversed array is same as the original.
38+
```
39+
40+
**Constraints:
41+
**1 ≤ arr.size() ≤ 10 <sup>5</sup>
42+
0 ≤ arr\[i\] ≤ 10 <sup>5</sup>
43+
44+
45+
46+
If you are facing any issue on this page. Please let us know.
47+
48+
---
49+
50+
## Solution
51+
52+
### Intuition
53+
54+
### Approach
55+
56+
### Complexity
57+
- Time:
58+
- Space:
59+
60+
### Code
61+
---
62+
[!]
63+
```cpp
64+
class Solution {
65+
public:
66+
void reverseArray(vector<int> &arr) {
67+
int start = 0;
68+
int end = arr.size() - 1;
69+
int temp;
70+
while (start < end) {
71+
temp = arr[start];
72+
arr[start] = arr[end];
73+
arr[end] = temp;
74+
start++;
75+
end--;
76+
}
77+
}
78+
};
79+
```
80+
81+
### Optimal Code
82+
---
83+
[!]
84+
```cpp
85+
class Solution {
86+
public:
87+
void reverseArray(vector<int> &arr) {
88+
int start = 0;
89+
int end = arr.size() - 1;
90+
91+
while (start < end) {
92+
std::swap(arr[start], arr[end]);
93+
start++;
94+
end--;
95+
}
96+
}
97+
};
98+
99+
```
100+

0 commit comments

Comments
 (0)