Left Rotation

  • + 0 comments

    C++ Solution:

    vector rotateLeft(int d, vector arr) {

    auto calcOldIndex = [](int newIndex, int shift, int size) 
    {
        int tmp = newIndex + shift;
        if(tmp >= size)
            tmp = tmp - size;
        return tmp;
    };
    
    vector<int> res;
    
    for(int i = 0; i < (int)arr.size(); i++){
        res.push_back(arr[calcOldIndex(i, d, arr.size())]);
    }
    return res;
    

    }