Array Rotation

Rotate an n-element vector left by i positions in time proportional to n with just a dozen bytes of extra space. For instance, with n=8 and i=3, the vector abcdefgh is rotated to defghabc

1 comment:

h'spec said...

1. Reverse the string : strrev(str) → hgfedcba
2. Reverse the substring (0,n-i) → defghcba
3. Reverse the second substring (n-i+1,n) → defghabc