Menu Close

[HackerRank] Left rotation array

Problem

link: https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem

Solution

// Complete the rotLeft function below.
vector<int> rotLeft(vector<int> a, int d) {
    vector<int> rotArr(a.size(),0);
    for(size_t i = 0; i < a.size(); i++){
        int newIndex = i - d;
        if (newIndex < 0) {
            newIndex = a.size() + newIndex;
        }
        rotArr[newIndex] = a[i];
    }
    return rotArr;
}

Leave a Reply

Your email address will not be published. Required fields are marked *