# Left rotation of an array using vectors in C++

Given an array **arr[]** of integers and another integer **D**, the task is to perform **D** left rotations on the array and print the modified array.

**Examples:**

Input:arr[] = {1, 2, 3, 4, 5, 6}, D = 2Output:3 4 5 6 1 2Input:arr[] = {1, 2, 3, 4, 5, 6}, D = 12Output:1 2 3 4 5 6

**Approach:** Using vectors in C++, a rotation can be performed by removing the first element from the vector and then inserting it in the end of the same vector. Similarly, all the requried rotations can be performed and then print the contents of the modified vector to get the required rotated array.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to left rotate the array by d elements ` `void` `rotate(vector<` `int` `> vec, ` `int` `d) ` `{ ` ` ` ` ` `// Push first d elements from the beginning ` ` ` `// to the end and remove those elements ` ` ` `// from the beginning ` ` ` `for` `(` `int` `i = 0; i < d; i++) { ` ` ` `vec.push_back(vec[0]); ` ` ` `vec.erase(vec.begin()); ` ` ` `} ` ` ` ` ` `// Print the rotated array ` ` ` `for` `(` `int` `i = 0; i < vec.size(); i++) { ` ` ` `cout << vec[i] << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `vector<` `int` `> vec = { 1, 2, 3, 4, 5, 6 }; ` ` ` `int` `n = vec.size(); ` ` ` `int` `d = 2; ` ` ` ` ` `rotate(vec, d % n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3 4 5 6 1 2

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Print left rotation of array in O(n) time and O(1) space
- Left Rotation and Right Rotation of a String
- Program for array rotation
- Reversal algorithm for array rotation
- Circular rotation of an array using deque in C++
- Reversal algorithm for right rotation of an array
- Block swap algorithm for array rotation
- C Program for Reversal algorithm for array rotation
- Find an element in array such that sum of left array is equal to sum of right array
- Find the Rotation Count in Rotated Sorted array
- Java Program for Reversal algorithm for array rotation
- Generate Array whose difference of each element with its left yields the given Array
- Check if an array can be Arranged in Left or Right Positioned Array
- Find Array formed by adding each element of given array with largest element in new array to its left
- Remove all 1s from the adjacent left of 0s in a Binary Array
- Queries on Left and Right Circular shift on array
- Closest greater or same value on left side for every element in array
- Minimum element left from the array after performing given operations
- Quickly find multiple left rotations of an array | Set 1
- Find the index of the left pointer after possible moves in the array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.