-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rotate Array.cpp
62 lines (52 loc) · 1.1 KB
/
Rotate Array.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
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution{
public:
//Function to rotate an array by d elements in counter-clockwise direction.
void rotateArr(int arr[], int d, int n){
// code here
if(d == 1)
{
int temp;
temp = arr[n-1];
arr[n-1] = arr[0];
arr[0] = temp;
return;
}
int temp[d];
for(int i =0; i < d; i++) temp[i] = arr[i];
for(int i=d; i < n; i++) arr[i-d] = arr[i];
int a=0;
for(int i=n-d; i < n; i++)
{
arr[i] = temp[a];
a++;
}
}
};
//{ Driver Code Starts.
int main() {
int t;
//taking testcases
cin >> t;
while(t--){
int n, d;
//input n and d
cin >> n >> d;
int arr[n];
//inserting elements in the array
for(int i = 0; i < n; i++){
cin >> arr[i];
}
Solution ob;
//calling rotateArr() function
ob.rotateArr(arr, d,n);
//printing the elements of the array
for(int i =0;i<n;i++){
cout << arr[i] << " ";
}
cout << endl;
}
return 0;
}