Sunday, 16 October 2016

Left Rotation (CTCI)

Problem: Left Rotation
Given an array of  integers and a number, , perform  left rotations on the array.

vector<int> array_left_rotation(vector<int> a, int n, int k) {
    k = k%n;
    int startPosition = k;
    vector<int>temporary;
    for(int i=0;i<n;i++)
    {
        temporary.push_back(a[startPosition]);
        startPosition = (startPosition+1)%n;
    }
    return temporary;    
}

No comments:

Post a Comment