-
Notifications
You must be signed in to change notification settings - Fork 125
/
Rotate_Array.cpp
66 lines (51 loc) · 1.2 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
63
64
65
66
//gfg problem
// Rotate Array
// Given an unsorted array arr[] of size N, rotate it by D elements in the counter-clockwise direction.
#include<bits/stdc++.h>
using namespace std;
class Solution{
public:
void reverse(int arr[], int i, int j )
{ int li=i;
int ri=j;
while(li<ri)
{
int temp =arr[li];
arr[li]=arr[ri];
arr[ri]=temp;
li++;
ri--;
}
}
//Function to rotate an array by d elements in counter-clockwise direction.
void rotateArr(int arr[], int d, int n){
reverse(arr,0,d-1);
reverse(arr,d,n-1);
reverse(arr,0,n-1);
}
};
// { 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;
} // } Driver Code Ends