-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRevers_an_Array.cpp
More file actions
77 lines (57 loc) · 1.62 KB
/
Copy pathRevers_an_Array.cpp
File metadata and controls
77 lines (57 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Reverse an Array
You are given an array of integers arr[]. Your task is to reverse the given array.
Examples:
Input: arr = [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: arr = [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: arr = [1]
Output: [1]
Explanation: The array has only single element, hence the reversed array is same as the original.
Constraints:
1<=arr.size()<=105
0<=arr[i]<=105
*/
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
void reverseArray(vector<int>& arr) {
int n = arr.size();
int j = 0;
vector<int> arr1(n);
for (int i = n - 1; i >= 0; i--) {
arr1[j] = arr[i];
j++;
}
for (int j = 0; j < n; j++) {
arr[j] = arr1[j];
}
}
};
int main() {
int t;
cin >> t;
cin.ignore();
while (t--) {
vector<int> arr;
string input;
getline(cin, input);
stringstream ss(input);
int number;
while (ss >> number) {
arr.push_back(number);
}
Solution ob;
ob.reverseArray(arr);
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << " ";
}
cout << endl;
}
cout << "~" << endl;
return 0;
}