-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeftRotation.cpp
More file actions
64 lines (56 loc) · 1.49 KB
/
LeftRotation.cpp
File metadata and controls
64 lines (56 loc) · 1.49 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
/* Rotate a given array arr d steps to the left.
https://www.hackerrank.com/challenges/array-left-rotation/problem*/
#include <vector>
#include <iostream>
#include <string>
using namespace std;
vector <int> leftRotation(vector<int> arr, int d)
{
// Reverse the array
for(int i = 0; i < arr.size()/2; i++)
{
int temp = arr[i];
arr[i] = arr[arr.size() - i - 1];
arr[arr.size() - i - 1] = temp;
}
// Reverse the first half
for(int i = 0; i < (arr.size() - d) / 2; i++)
{
int temp = arr[i];
arr[i] = arr[arr.size() - i - 1 - d];
arr[arr.size() - i - 1 - d] = temp;
}
// O(n) time complexity
// Reverse the second half
for(int j = 0, i = arr.size() - d; i < arr.size()- d/2; j++, i++)
{
int temp = arr[i];
arr[i] = arr[arr.size() - j - 1];
arr[arr.size() - j - 1] = temp;
}
for(int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return arr;
// Quadratic time complexity
// for(int i = 0; i < d; i++)
// {
// int temp = arr[0];
// //cout << temp << " is temp" << endl;
// arr.erase(arr.begin() + 0);
// arr.push_back(temp);
// }
// for(int i = 0; i < arr.size(); i++)
// cout << arr[i] << " ";
// return arr;
}
int main()
{
int size, rotation;
cin >> size;
cin >> rotation;
vector<int> a(size);
for (int i = 0; i < size; i++)
cin >> a[i];
leftRotation(a, rotation);
return 0;
}