-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInvertPosition.cpp
More file actions
56 lines (44 loc) · 1020 Bytes
/
Copy pathInvertPosition.cpp
File metadata and controls
56 lines (44 loc) · 1020 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
50
51
52
53
54
55
56
/*
Invert the Position of Elements in an Array
Definition
Given an array, you have to reverse the position of array elements.
Solution:
Try to solve this using multiple approach
*/
#define FORREF
#ifndef FORREF
using namespace std;
#include<iostream>
struct InvertPosition {
void reverse_iter(int arr[], int left, int right) {
while (left < right) {
swap(arr[left], arr[right]);
++left;
--right;
}
}
void reverse(int arr[], int left, int right) {
if (left >= right)
return;
else
{
swap(arr[left], arr[right]);
reverse(arr, left + 1, right - 1);
}
}
};
//Driver function
int main()
{
int array[5] = { 1,2,3,4,5 };//define the array
int size = 5;//define the size of array
InvertPosition ip;
ip.reverse(array, 0, size - 1);//call the recursive function
//reverseArray(array, 0, size - 1); //call the recursive function
cout << "Reversed Array:";
for (int i = 0; i < 5; i++)
cout << array[i] << " ";//print the array
cout << endl;
return 0;
}
#endif // FORREF