forked from anmol098/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_rotation.cpp
85 lines (75 loc) · 1.73 KB
/
array_rotation.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <cassert>
void swap( int & a, int & b)
{
int temp = a;
a = b;
b = temp;
}
/**
* reverse an array from index 'start' to index 'end'
* @param arr [The array]
* @param start [The start index]
* @param end [The end index]
*/
void reverse( int * arr, int start, int end )
{
while( start < end ) {
swap(arr[start], arr[end]);
++start;
--end;
}
}
/**
* [leftRotate - rotate an array of length n with r rotations in left direction
* @param arr - the array
* @param n - size of array
* @param r - n number of rotations
*/
void leftRotate( int* arr, int n, int r )
{
r = r % n;
reverse( arr, 0, r-1);
reverse( arr, r, n-1);
reverse( arr, 0, n-1);
}
/**
* [rightRotate - rotate an array of length n with r rotations in left direction
* @param arr - the array
* @param n - size of array
* @param r - n number of rotations
*/
void rightRotate( int* arr, int n, int r )
{
r = r % n;
reverse( arr, 0, n-1);
reverse( arr, 0, r-1);
reverse( arr, r, n-1);
}
int main()
{
int n, r, d;
std::cout << "Enter size of array :";
std::cin >> n;
std::cout << "Enter the contents of array:";
int * arr = new int[n];
for ( int i = 0; i < n; ++i ) {
std::cin >> arr[i];
}
std::cout << "Enter number of rotations you need:";
std::cin >> r;
std::cout << "Direction (left = 0, right = 1):";
std::cin >> d;
assert((d == 0) || (d == 1));
if ( d == 0 ) {
leftRotate(arr, n, r);
} else {
rightRotate(arr, n, r);
}
std::cout << "The array after rotation:\n";
for ( int i = 0; i < n; ++i ) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}