Skip to content

Instantly share code, notes, and snippets.

@hckim16
Last active February 4, 2018 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hckim16/17090578459b7075be62407bd6f303f0 to your computer and use it in GitHub Desktop.
Save hckim16/17090578459b7075be62407bd6f303f0 to your computer and use it in GitHub Desktop.
left rotation using standard libraries - vector and algorithm
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> left_rotate(vector<int>array, int r){
rotate(array.begin(), array.begin()+r, array.end());
return array;
}
int main(){
int n;
int r;
cout << "Enter number of integers in array: ";
cin >> n;
cout << endl;
cout << "Enter number of rotations to left: ";
cin >> r;
cout << endl;
vector<int> arr(n);
for(int i = 0; i < n; i++){
cout << "Enter integer: ";
cin >> arr[i];
}
for(int i = 0; i < n; i++){
cout << arr[i] << " ";
}
cout << endl;
vector<int> newArr = left_rotate(arr, r);
for(int i = 0; i < n; i++){
cout << newArr[i] << " ";
}
cout << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment