Left Rotation

Sort by

recency

|

145 Discussions

|

  • + 0 comments

    one line finisher return arr[d:]+arr[:d]

  • + 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;
    

    }

  • + 0 comments

    python 3

    def rotateLeft(d, arr):
    return map(lambda x: arr[(x[0]+d) % len(arr)], enumerate(arr))
    
  • + 0 comments

    My rust solution:

    fn rotateLeft(d: i32, arr: &mut [i32]) {
        arr.rotate_left(d as usize);
    }
    
  • + 0 comments
    def rotateLeft(d, arr):
        # Write your code here
        for i in range(d):
            arr.append(arr[i])
        return arr[d:]