forked from supershivam13/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateArrayInO(n).cpp
More file actions
81 lines (56 loc) · 1.18 KB
/
RotateArrayInO(n).cpp
File metadata and controls
81 lines (56 loc) · 1.18 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
void rotateArr(int arr[], int d, int n){
// HINT ->
// reverse(a, a+d) Reverse array from beginning till D
// reverse(a+d, a+n) Reverse array from D till N
// reverse(a, a+n) Reverse the whole array
int l=0;
int r=d-1;
while(l<r){
int t=arr[l];
arr[l]=arr[r];
arr[r]=t;
l++;
r--;
}
int l1=d;
int r1=n-1;
while(l1<r1){
int t=arr[l1];
arr[l1]=arr[r1];
arr[r1]=t;
l1++;
r1--;
}
int l2=0;
int r2=n-1;
while(l2<r2){
int t=arr[l2];
arr[l2]=arr[r2];
arr[r2]=t;
l2++;
r2--;
}
}
// { Driver Code Starts.
int main() {
int t;
cin >> t;
while(t--){
int n, d;
cin >> n >> d;
int arr[n];
for(int i = 0; i < n; i++){
cin >> arr[i];
}
rotateArr(arr, d,n);
for(int i =0;i<n;i++){
cout << arr[i] << " ";
}
cout << endl;
}
return 0;
} // } Driver Code Ends