Blog on algorithms, puzzles, programming .... musings of a computer engineer.
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:
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
Post a Comment