forked from TECHOUS/DSKaKhel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotate_Array_Cyclically.cpp
More file actions
55 lines (37 loc) · 959 Bytes
/
rotate_Array_Cyclically.cpp
File metadata and controls
55 lines (37 loc) · 959 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
#include<iostream>
#include<limits>
#include<algorithm>
using namespace std;
/* Time complexity of my programme is O(n * k);
k stands for the no of rotations needed
*/
void cyclicallyRotateArray(int *arr,int size,int k){
while(k--){
int temp = arr[size-1];
for(int i = size-1; i > 0 ; i--){
arr[i] = arr[i-1];
}
arr[0] = temp;
}
}
void printArray(int *arr,int size){
for(int i = 0 ; i < size ; i++){
cout<<arr[i]<<endl;
}
}
int main(){
int size,k;
cout<<"Enter the size"<<endl;
cin>>size;
cout<<"Enter the no of rotations"<<endl;
cin>>k;
int *arr = new int(size);
cout<<"Enter the elements of the array"<<endl;
for(int i = 0 ; i < size ; i++){
cin>>arr[i];
}
cout<<"Rotating the array cyclically"<<endl;
cyclicallyRotateArray(arr,size,k);
cout<<"The array after rotating is "<<endl;
printArray(arr,size);
}