Skip to content

Instantly share code, notes, and snippets.

@mafayaz
Created October 18, 2016 09:55
Show Gist options
  • Save mafayaz/d5cb5efe8d8f6837f32608f7725c704a to your computer and use it in GitHub Desktop.
Save mafayaz/d5cb5efe8d8f6837f32608f7725c704a to your computer and use it in GitHub Desktop.
Left rotate array
#include <boost/assign/list_of.hpp>
#include <vector>
using namespace std;
vector<int> array_left_rotation(vector<int> a, int n, int k) {
vector<int> rotated;
int index = k % n;
for(int i=index; i<n; i++)
{
rotated.push_back(a[i]);
}
for(int i=0; i<index; i++)
{
rotated.push_back(a[i]);
}
return rotated;
}
int main(){
std::vector<int> input = boost::assign::list_of(1)(2)(3)(4)(5);
vector<int> output = array_left_rotation(input, input.size(), 6);
for(int i = 0; i < input.size();i++)
cout << output[i] << " ";
cout << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment