File tree 1 file changed +57
-0
lines changed
1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ // using stl (M-1)
2
+ #include < bits/stdc++.h>
3
+ using namespace std ;
4
+
5
+ int main (){
6
+ int n;
7
+ cin>>n;
8
+ int arr[n];
9
+ for (int i=0 ; i<n; i++) cin>>arr[i];
10
+ reverse (arr,arr+n);
11
+ for (int i=0 ; i<n; i++) cout<<arr[i]" " ;
12
+ }
13
+
14
+ // Iterative way (M-2)
15
+ #include < bits/stdc++.h>
16
+ using namespace std ;
17
+
18
+ void reverse (int arr[], int n){
19
+ int start=0 ;
20
+ int end=n-1 ;
21
+ while (start<=end){
22
+ swap (arr[start],arr[end]);
23
+ start++;
24
+ end--;
25
+ }
26
+ for (int i=0 ; i<n; i++) cout<<arr[n]<<" " ;
27
+ }
28
+
29
+ int main (){
30
+ int n;
31
+ cin>>n;
32
+ int arr[n];
33
+ for (int i=0 ; i<n; i++) cin>>arr[i];
34
+ reverse (arr,n);
35
+ }
36
+
37
+ // Recursive way (M-3)
38
+ #include < bits/stdc++.h>
39
+ using namespace std ;
40
+
41
+ void reverse (int arr[], int n, int start, int end){
42
+ while (start<=end){
43
+ swap (arr[start],arr[end]);
44
+ start++;
45
+ end--;
46
+ reverse (arr,n,start+1 ,end-1 );
47
+ }
48
+ }
49
+
50
+ int main (){
51
+ int n;
52
+ cin>>n;
53
+ int arr[n];
54
+ for (int i=0 ; i<n; i++) cin>>arr[i];
55
+ reverse (arr,n,0 ,n-1 );
56
+ for (int i=0 ; i<n; i++) cout<<arr[i]<<" " ;
57
+ }
You can’t perform that action at this time.
0 commit comments