forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cyclically_rotate_of_array_by_one.cpp
81 lines (73 loc) · 1.27 KB
/
Cyclically_rotate_of_array_by_one.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
/*
Description :
Given an array and rotate it by one position in clock-wise direction.
*/
#include <iostream>
using namespace std;
//function is used to rotate the array
void rotate(int a[], int n)
{
int x = a[n - 1];
//starting the loop from backside of array
for (int i = n - 1; i > 0; i--)
{
a[i] = a[i - 1];
}
//storing first element in x
a[0] = x;
}
int main()
{
int n;
cout << "Enter the size of an array : " << endl;
cin >> n;
int a[n];
cout << "Enter " << n << " number of elements : " << endl;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
cout << "Cyclically rotate of an array by one : " << endl;
rotate(a, n);
//printing array
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
return 0;
}
/*
Time complexity : O(n)
Space complexity : O(n)
*/
/*
Test Case :
Test Case 1 :
Input :
Enter the size of an array :
4
Enter 4 number of elements :
1
2
3
4
Output :
Cyclically rotate of an array by one :
4 1 2 3
Test Case 2 :
Input :
Enter the size of an array :
8
Enter 8 number of elements :
9
8
7
6
4
2
1
3
Output :
Cyclically rotate of an array by one :
3 9 8 7 6 4 2 1
*/