Arrays and strings · Exercise 12 of 100

Right Rotation

One right rotation moves the last value to the front and shifts every other value one slot to the right. For a nonempty array, each full round of shifts brings every value back to its starting slot, so you can skip whole rounds.

Try it yourself

The names [ada, ben, cam, dee, eli] rotate right by 12 slots. Which name ends up first?

One-step hint

A full turn puts every name back in place. Reduce 12 by whole turns, then trace the first slot backward by the remaining distance.

Worked solution
  1. There are five names, so 10 shifts make two full rounds. Only 2 shifts remain.
  2. The first remaining shift moves eli to the front and leaves dee last. The next shift puts dee first.
DSA compiler

Explore the idea visually in the array indexes lab.