-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayRotationByRecursion.cpp
More file actions
59 lines (53 loc) · 1.27 KB
/
ArrayRotationByRecursion.cpp
File metadata and controls
59 lines (53 loc) · 1.27 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
/*
Array = {1 2 3 4 5 6} d=3
First reverse (0,d-1) = {3 2 1} Second reverse (d,n-1) = {6 5 4}
Whole reverse(0,n-1) = {4 5 6 1 2 3}*/
#include <iostream>
using namespace std;
int arr_n;
void arrayReverse(int local_arr[], int start, int end)
{
int temp;
while (start < end)
{
temp = local_arr[start];
local_arr[start] = local_arr[end];
local_arr[end] = temp;
++start;
--end;
}
}
void rotateArrayLeft(int local_arr[], int d)
{
arrayReverse(local_arr, 0, d - 1);
/*cout<<"\nLocal Array after 1 Rotation is :";
for(int i=0;i<arr_n;i++)
cout<<local_arr[i]<<" ";
*/
arrayReverse(local_arr, d, arr_n - 1);
/*cout<<"\nLocal Array after 2 Rotation is :";
for(int i=0;i<arr_n;i++)
cout<<local_arr[i]<<" ";*/
arrayReverse(local_arr, 0, arr_n - 1);
/*cout<<"\nLocal Array after 3rd Rotation is :";
for(int i=0;i<arr_n;i++)
cout<<local_arr[i]<<" ";*/
}
int main()
{
int arr[50], d;
cout << "Number of values in array:";
cin >> arr_n;
cout << "\nEnter " << arr_n << " values :";
for (int i = 0; i < arr_n; i++)
arr[i] = 0;
for (int i = 0; i < arr_n; i++)
cin >> arr[i];
cout << "\nNumber elements wants rotate:";
cin >> d;
rotateArrayLeft(arr, d);
cout << "\nArray after Rotation is :";
for (int i = 0; i < arr_n; i++)
cout << arr[i] << " ";
return 0;
}