Created
May 15, 2020 01:29
-
-
Save misterpoloy/da45335bf270c21370549c3094cf4862 to your computer and use it in GitHub Desktop.
Rotate n times an array elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vector<int> rotLeft(vector<int> a, int d) { | |
vector<int> rotations(a.size(), -1); | |
for (int i = 0; i < a.size(); i++) { | |
// Get right moves formula = a.length - left Rotaions | |
int rightMoves = a.size() - d; | |
// Get the next index position of our current number | |
int newIndex = (i + rightMoves) % a.size(); // Circular array right | |
rotations[newIndex] = a[i]; | |
} | |
return rotations; | |
} |
Author
misterpoloy
commented
May 15, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment