All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars
 perplexus dot info

 Rotating an array (Posted on 2004-04-19)
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.

 See The Solution Submitted by e.g. Rating: 4.0000 (13 votes)

Comments: ( Back to comment list | You must be logged in to post comments.)
 3 Step Method | Comment 11 of 26 |
Surely the answer is more simple than any we have seen,

The first move should be to flip the entire array:- FLIP(1,M)
the second would be to then FLIP(1,N)
the final move would be to FLIP(N+1,M).

A-B-C-D-E-F-G-H-I-J-K
K-J-I-H-G-F-E-D-C-B-A
I-J-K-H-G-F-E-D-C-B-A
I-J-K-A-B-C-D-E-F-G-H

the above works because you are inverting all the letters in between your to numbers, see e.g. in the question.

"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".

 Posted by Juggler on 2004-04-19 18:40:49

 Search: Search body:
Forums (2)