Open In App

Left Rotation in Vectors in C++

Last Updated : 29 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1, 3, 6, 2, 9};
    int n = v.size();
    int d = 2;

    // Left rotate the vector by d place
    rotate(v.begin(), v.begin() + d, v.end());

    for (auto i : v)
        cout << i << " ";
    return 0;
}

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.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1, 3, 6, 2, 9};
    int n = v.size();
    int d = 2;

    // Left rotate the vector by d place
    reverse(v.begin(), v.begin() + d);
    reverse(v.begin() + d, v.end());
    reverse(v.begin(), v.end());

    for (auto i : v)
        cout << i << " ";
    return 0;
}

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.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1, 3, 6, 2, 9};
    int n = v.size();
    int d = 2;

    // Create temporary vector
    vector<int> t;

    // Insert elements from index [d, size)
    t.insert(t.end(), v.begin() + d, v.end());
  
  	// Insert elements from index [0, d)
  	t.insert(t.end(), v.begin(), v.begin() + d);

    v = t;
    for (auto i : v)
        cout << i << " ";
    return 0;
}

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.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1, 3, 6, 2, 9};
    int n = v.size();
    int d = 2;

    // Left rotate the vector by d place
    v.insert(v.end(), v.begin(), v.begin() + d);
    v.erase(v.begin(), v.begin() + d);

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
6 2 9 1 3 




Next Article
Article Tags :
Practice Tags :

Similar Reads