-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseArray.cpp
More file actions
49 lines (46 loc) · 900 Bytes
/
ReverseArray.cpp
File metadata and controls
49 lines (46 loc) · 900 Bytes
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
#include <bits/stdc++.h>
using namespace std;
void printArray(int arr[], int n)
{
int i = 0;
while (i < n)
{
cout << arr[i++] << " ";
}
}
// USING ITERATIVE METHOD
void reverseArray(int arr[], int n)
{
int start = 0;
int end = n-1;
while (start < end)
{
swap(arr[start++], arr[end--]);
}
cout<<"ITERATIVELY REVERSED ARRAY : ";
printArray(arr, n);
}
//USING RECURSION
void reverseArray(int arr[],int start,int end)
{
if(start>=end)
return;
swap(arr[start],arr[end]);
reverseArray(arr,start+1,end-1);
}
int main()
{
int n, i = 0;
cout << "Enter the number of Elements : ";
cin >> n;
int arr[n];
while (i < n)
{
cin >> arr[i++];
}
reverseArray(arr, n);
cout<<endl;
cout<<"RECURSIVELY REVERSED ARRAY : ";
reverseArray(arr,0, n-1);
printArray(arr, n);
}