-
Notifications
You must be signed in to change notification settings - Fork 342
/
Left rotate an array k times.cpp
58 lines (44 loc) · 1.11 KB
/
Left rotate an array k times.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
#include<bits/stdc++.h>
using namespace std;
void leftRotate(int arr[], int d, int n)
{
d = d % n; //handle if d >=n
int g_c_d = __gcd(d, n); // __gcd predefine method in c++.which takes two input and return gcd.
for (int i = 0; i < g_c_d; i++) {
int temp = arr[i]; // move i-th values of blocks
int j = i;
while (1) {
int k = j + d;
if (k >= n)
k = k - n;
if (k == i)
break;
arr[j] = arr[k];
j = k;
}
arr[j] = temp;
}
}
void printArray(int arr[], int size) // Function to print an array
{
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout<<endl;
}
int main()
{
int n,k;
cout<<"Enter size of an array and value of k:\n";
cin>>n>>k;
int arr[n];
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Before Rotation:\n";
printArray(arr, n);
leftRotate(arr,k, n); // Function calling
cout<<"After Rotation:\n";
printArray(arr, n);
return 0;
} //Time complexity: o(n) and space complexity:o(1)