I have an array such as
A-B-C-D-E-F-G-H-I-J-K
I want to rotate it N places to the right; for example, if N=3, the array should end
I-J-K-A-B-C-D-E-F-G-H
Assume that the only available operation is a FLIP method that can invert any portion of the array. For example, applied to the original array FLIP(3,6) would produce A-B-F-E-D-C-G-H-I-J-K.
(In reply to
Algorithm by Penny)
Penny is correct, this will work.
But it takes N x (M-N) flips (where N is the offset, and M is the number of elements in the array).
In Penny's example, this takes 4 x (11-4) = 28 flips.
This *can* be done in no more than M-1 flips (or in the case given in the problem in 10 flips), regardless of N, the offset.