Today I stumbled across an interesting problem. The concern is that we should rotate a one-dimensional vector of $n$ element left by $d$ ($\Delta$) positions.

# Problem Statement

For instance, say we have an array with $n=8$ elements should rotate it by $d=3$ shifts. For example, vector [1,2,3,4,5,6,7,8] is rotated to [4,5,6,7,8,1,2,3].

A naive algorithm will use an n-element intermediate vector to do the job in $n$ steps or copy the underlying array with language dependant slicing features (where space is $O(n)$) or compute in-place shift by holding a temporary variable with $d$ iterations (which will take a running time of $O(\Delta * n)$).

But we are better than this! can we rotate the vector in time proportional to $O(n)$ without additional memory allocations $O(1)$?

# Aha!

Yes, we can! Thanks to Jon Bentley, in his Programming Pearls (2nd edition) book, he mentioned three vector rotation algorithms called juggling, swapping, and reversing.

Jon derived a smart insight in which he calls the aha! moment! because, in the reverse rotation technique, it is basically two arrays reversed twice. Truly marvelous!

The reversing algorithm is pretty straightforward, and the other two are very clever. But for this code fragment, I wil only go through the reversing algorithm because that's the most elegant code. Jon makes use of a bidirectional iterator to perform the task in $o(n)$ time.

Now let's dive into the algorithm! woohoo! 🥳🕺

# Reverse Rotate

Recall the example in the problem description. we have to left rotate the vector [1,2,3,4,5,6,7,8] by $3$. If we view it as two sub-vectors, we get $3$ and $5$ items long sub-vector: [1,2,3]-[4,5,6,7,8].

In jon's algorithm, to rotate the vector, we reverse the first sub-vector to obtain [3,2,1,4,5,6,7,8]. Essentially, the first reverse of the sub-vector a is swapping the positions of each element until delta.

Then, reverse the second sub-vector b to obtain [3,2,1,8,7,6,5,4]. like previously, we make use of the two-pointers to swap them in-place without additional space overhead.

Finally, reverse the whole thing to obtain [4,5,6,7,8,1,2,3].

That's it! how cool is that, huh?

# Implementation

Here's a golang implementation of this algorithm. The reverse function takes a pointer to the array $x$ and bidirectional iterator positions $i$ and $j$ for swapping the indexes. Then, we loop $\sum_{i}^{j}$ until $i \lt j$ to swap $x_i$ and $x_j$, and consecutively we increment $i$ and decrement $j$ by one.

Then comes the real magic. Inside the reverseRotate function we invoke the reverse routine $3$ times. The first call is to get $A'$ (we start from $0$ for $i$, and $j$ is always $\lt\Delta$), and then the second call is to derive $B'$ (we start from $\Delta$ for $i$ and swap until $n-1$). At this point, we only need to reverse the entire array once from $0$ to $n - 1$ to obtain the rotated vector.

This is an elegant way to solve the problem, especially considering one vector as a sum of two (Prakhar Srivastav, 2014). In Aha! Algorithms Research Paper Jon mentions that this reversal code is guaranteed time and space-efficient and is so short and so damn simple that it's pretty hard to get wrong.

It is precisely the code that Brian Kernighan & Bill Plauger use in their Software Tools book's editor and also what Ken Thompson used in his text editor ed to manipulate lines.

# Stay Curious

These are some interesting resources I referred while writing this post. Be sure to check these posts from great engineers out there!

Thanks a million for reading! until next time!

# Well, now what?

You can navigate to more writings from here. Connect with me on LinkedIn for a chat.

1. # 2023

## August

1. TypeScript's Omit & Pick
10th
2. ## June

1. reverse() vs toReversed()
28th
3. ## May

1. Integrating JUnit in Maven Projects
25th

1. # 2022

## December

1. Synchronizing time
31st

3. ## September

1. Asgardeo Try it Application
6th
4. ## August

1. Flatten error constraints
11th
5. ## July

1. Good Git commit messages
24th
6. ## March

1. Asgardeo JIT User Provisioning
9th