Arrays: Left Rotation

  • + 0 comments

    Indeed, its not in-place at all. Also its slow. If you are interested, here's an inplace c++ solution

    void rotLeft(vector<int> &a, int d) {
        int c=a[d],ci=0,n=a.size(); a[d]=0;
        for(int i=n-1;i>=0;i--){
            swap(a[ci],c);
            if(!c&&i) ci+=n-i+++i;
            ci=(n+ci-d)%n;
        }
    }