-
Notifications
You must be signed in to change notification settings - Fork 111
/
Array_rotation
67 lines (51 loc) · 920 Bytes
/
Array_rotation
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
#include <iostream>
using namespace std;
void reverse(int arr[],int l,int h)
{
int temp;
while(l<h)
{
temp = arr[l];
arr[l]= arr[h];
arr[h]= temp;
l++;
h--;
}
}
void left_rotate_array(int arr[],int n,int d)
{
d=d%n; //For corner case when d>n
reverse(arr,0,d-1);
reverse(arr,d,n-1);
reverse(arr,0,n-1);
}
void right_rotate_array(int a[],int n,int d)
{
d=d%n;
reverse(a,0,n-1);
reverse(a,0,d-1);
reverse(a,d,n-1);
}
int main() {
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int d;
cin>>d;
left_rotate_array(a,n,d);
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
right_rotate_array(a,n,d);
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
return 0;
}