Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created May 15, 2020 01:29
Show Gist options
  • Save misterpoloy/da45335bf270c21370549c3094cf4862 to your computer and use it in GitHub Desktop.
Save misterpoloy/da45335bf270c21370549c3094cf4862 to your computer and use it in GitHub Desktop.
Rotate n times an array elements
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;
}
@misterpoloy
Copy link
Author

LeftRotationArray

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment