VOOZH about

URL: https://www.geeksforgeeks.org/dsa/left-rotation-of-an-array-using-vectors-in-c/

⇱ Left Rotation in Vectors in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Left Rotation in Vectors in C++

Last Updated : 12 Jul, 2025

Left rotation of a vector means shifting all the elements of the vector to the left by a specified number of positions. The elements that are shifted out from the left end are wrapped around to the right end of the vector. In this article, we will learn how to left rotate a vector by given number of positions.

The simplest method to perform the left rotation on vector is by using rotate() function. Let’s take a look at a simple example:


Output
6 2 9 1 3 

Explanation: In rotate() function, the first argument is the beginning of the vector, the second is the position where the rotation starts, and the third is the end of the vector.

Note: If d become greater than size of vector, then we take the modulo with size of vector to make d will be in the range.

There are also some other methods in C++ to left rotate the vector by d place. Some of them are as follows:

Using reverse()

The left rotation of vector by d elements can be done by reversing three parts of the vector using reverse() function. First reverse the first d elements, then reverse the remaining elements and finally reverse the entire vector to restore the correct order.


Output
6 2 9 1 3 

Using Vector Slicing

First create a temporary vector and insert elements from the dth position to the end of the original vector. Then append the elements from the start to the dth position of the original vector. The resultant vector will contain the left rotated elements.


Output
6 2 9 1 3 

Using Vector erase() with insert()

Erase first d elements of vector using vector erase() and insert it to the end of vector using vector insert() method.


Output
6 2 9 1 3 


Comment
Article Tags: