Arrays: Left Rotation

Sort by

recency

|

4981 Discussions

|

  • + 0 comments

    C++ solution

    vector<int> rotLeft(vector<int> a, int d) {
        while(d)
        {
            int temp = a[0];
            a.push_back(temp);
            a.erase(a.begin());
            d--;
        }
        
        return a;
    }
    
  • + 0 comments
        int n = a.Count;
    
        for (int i = 0; i < d; i++)
        {
            int firstElement = a[0];
            a.RemoveAt(0);
            a.Insert(n - 1, firstElement);
        }
    
        return a;
    
  • + 0 comments

    Python efficient using mod:

    def rotLeft(a, d): 
        realRots = d % len(a) 
        return a[realRots:] + a[:realRots]
    
  • + 0 comments

    Python:

    def rotLeft(a, d):
        # Write your code here
        for i in range(d):
            item = a.pop(0)
            a.append(item)
        return a
    
  • + 0 comments

    Python:

        for i in range(d):
            item = a.pop(0)
            a.append(item)
        return a