Line up the 2 6 letters in the English alphabet randomly in a line. The alphabet letters are not necessarily in sorted order. To bring the letters back into sorted order, you are only allowed to swap two letters. For example, you can swap the two letters a , b by sending a to b , and b to a .
What is the maximum number of swaps you need to bring the letters back to sorted order. If you may never be able to bring the letters back into sorted order, submit − 1 .
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
How can you be sure you can't do it in, say, 24 steps?
Log in to reply
If all letters have been moved one position out of place b c d … y z a , it cannot be done in less than 25 steps.
Log in to reply
Sure, that's an example I thought of as well, but how would you go about proving that it can't work in 24 steps?
For instance, you can't say each step can at most cover "fixing" one letter's position - consider dcba , which can be done in 2 steps.
Log in to reply
@Ivo Zerkov – Your example corresponds to the cycle decomposition ( 1 4 ) ( 2 3 ) . To order it into the identity permutation ( 1 ) ( 2 ) ( 3 ) ( 4 ) , only two cycles need to be added; therefore two steps are sufficient. However, the case b c d a , corresponding to a single cycle ( 1 2 3 4 ) , requires addition of three cycles, i.e. three steps.
Correct. It is a familiar result in the theory of permutation groups that an n -cycle can be written as the composition of n − 1 transpositions, but no fewer.
The outline of the proof is as follows. First, each permutation of the alphabet can be viewed (uniquely) as the composition of c disjoint cycles. The ordered alphabet corresponds to the identity permutation, which consists of 26 1-cycles: ( 1 ) ( 2 ) ( 3 ) ⋯ ( 2 6 ) .
Next, when composing a transposition τ = ( a b ) to a permutation σ , there are two possibilities. If the elements a , b belong to different cycles, the cycles are joined together into one, thus reducing the number of cycles from c to c − 1 : ( a b ) ∘ ( a x 1 x 2 … x m ) ( b y 1 y 2 … y n ) = ( a x 1 … x m b y 1 … y n ) . But if a , b belong to the same cycle, it is now split into two, thus increasing the number of cycles from c to c + 1 : ( a b ) ∘ ( a x 1 … x m b y 1 … y n ) = ( a x 1 x 2 … x m ) ( b y 1 y 2 … y n ) . Therefore each transposition changes the number of cycles by ± 1 ; and to increase the number of cycles, transpose two elements that belong to the same cycle in the disjoint-cycle decomposition.
The shifted alphabet b c d ⋯ y z a corresponds to the 26-cycle ( 1 2 … 2 6 ) ; the ordered alphabet a b c ⋯ x y z corresponds to 26 1-cycles ( 1 ) ( 2 ) … ( 2 6 ) ; to increase the number of cycles from 1 to 26, we need therefore at least 25 steps, and this minimum is accomplished if and only if every transposition exchanges two elements that belonged to the same cycle.
Problem Loading...
Note Loading...
Set Loading...
Go through the list from left to right.
Swap the first letter (whatever it is) with a .
Swap the second letter (whatever it is) with b .
Keep going: swap the n th letter (whatever it is) with the letter that belongs in that position.
Finally, swap the 25th letter (whatever it is) with y .
After the n th stage of this process, the first n letters will be placed correctly, and the remaining 2 6 − n letters will be in the remaining part of the list. After step 25, the remaining letter z is automatically in the right place!
Therefore it takes 2 5 steps at most.