You have 2017 cards numbered 1 , 2 , 3 , … , 2 0 1 7 , as shown above. In each move, you can swap two adjacent cards.
What is the minimum number of moves required for the above cards to be arranged backwards as 2 0 1 7 , 2 0 1 6 , … , 2 , 1 ?
As an explicit example, if you started with four cards arranged as 1234, in one move you could go to 2134, 1324, or 1243 only.
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.
As always, with finding max/min, we have to show
1) No higher/lower number works,
2) this number works.
For this problem, part 2 was easily guessed because almost any greedy algorithm / naive approach would give us the value. However, part 1 of justifying that we couldn't do better, was hard to explain. Most people struggled with this, saying that "We have to swap 1 and 2, so ultimately 2 still has to be moved 2016 times, instead of just 2015 times directly".
Thus, the hard part of the problem is to account for "1 has to get past 2, even though 2 would love to not get past 1". This suggests "Let's count the number of times when a smaller number is to the right of a larger number". At the start, it is 0, and at the end, it is 2033136. Hey, maybe this would be a good thing to track!
As you can see from the solution, this turns out to be exactly it. In fact, we can make a much stronger claim.
Corollary: If we make 2033136 moves such that each move sends a card with a lower number from the left to the right in any order , then we would end up with the final arrangement.
Followup: Can you determine the number of possible sequences of moves to achieve the final state? It might help to look at small values first, and make use of the corollary.
Formally: If m < n are two numbers in the sequence, let ψ ( m , n ) = 1 iff m lies to the right of n . Let Ψ : = m = 1 ∑ 2 0 1 6 n = m + 1 ∑ 2 0 1 7 ψ ( m , n ) . Observation 1: Initially, all cards are out of order: Ψ i = m = 1 ∑ 2 0 1 6 n = m + 1 ∑ 2 0 1 7 1 = 1 + 2 + ⋯ + 2 0 1 6 = 2 0 3 3 1 3 6 . Likewise, finally, all cards are in order: Ψ f = 0 .
Observation 2: As long as there are numbers out of order ( m < n and ψ ( m , n ) = 1 ), there are two neighboring numbers out of order.
Observation 3: Swapping two neighboring cards that are out of order decreases Ψ by one.
Conclusion: As long as the sequence is out of order, we can always find two neighboring numbers that are out of order. Swapping them always lowers Ψ by one. It is therefore possible to swap neighboring numbers until Ψ = 0 . This requires precisely 2 0 3 3 1 3 6 swaps. This is therefore both the minimum and the maximum number of steps.
Log in to reply
Oh woah! Proving the minimality and maximality in one proof!!! So good! I wonder how you come up with this algorithm....
Great! That's the "simple" proof that I was looking for.
For clarity, an inversion is a pair i < j such that π ( i ) < π ( j ) . We can verify by case checking that "a single swap increments or decrements the number of inversions by exactly one".
As such, this leads to the corollary: We can reach the final stage in only 2 0 3 3 1 3 6 + 2 n steps, where n is a non-negative integer. (The above proves why this is a necessary condition, and we have to find a series of steps that makes this a sufficient condition.)
The method of counting inversions (i.e. the number of paired cards where the left card is greater than the right card) leads to a simple process by which the order of cards is fully reversed: Pick any two adjacent cards which are not yet inverted and swap them. Repeat until every pair is inverted. To prove this method is possible we need to show that if there is any non-inverted pair still present within the row of cards, then there is at least one adjacent non-inverted pair. The easiest way to prove this is to observe that if every adjacent pair was inverted (a > b, b > c, c > d and so on) then the whole row of cards would be in reverse numerical order.
It seems that one method could be to move 2 0 1 7 first, such that it reaches the leftmost position- this would take 2 0 1 6 moves.
Next we could move the 2 0 1 6 card 2 0 1 5 spaces to the left, leaving it second in the line.
Repeating this for all cards from 2 0 1 7 down to 2 , the total number of moves is the sum of the numbers from 1 to 2 0 1 6 .
∑ r = 1 2 0 1 6 r = 2 1 × 2 0 1 6 × 2 0 1 7 = 2 0 3 3 1 3 6 .
Is this the optimum approach? I'll use "proof by it's-the-right-answer";)
[This comment has been converted into a solution]
Log in to reply
Yes, this is a valid way to actually prove optimality
You can also see Toshit's conversation with Calvin in the solution above to figure out why this is actually the optimal approach
Let's say we replace 2 0 1 7 with n . The hypothesis P ( n ) asserts that the optimal method takes ( ∑ r = 1 n − 1 r ) steps. P ( 2 ) is obviously true. We can combine the idea of contrapositive and induction together: suppose P ( k + 1 ) is false, namely there is a way to play this game in less than ( ∑ r = 1 n r ) steps. There must have been at least k times when the card with k + 1 has been swapped. Taking out all these swaps, we then effectively found a way to play the game with k cards in less than ( ∑ r = 1 k − 1 r ) steps, implying that P ( k ) is false. This leads to a contradiction , so P ( n ) is indeed true for any n ≥ 2 .
@Yong See Foo , we really liked your comment, and have converted it into a solution.
1 is at the first place , it comes to second place = 1 move | third place = 2 move | Therefore , No. Of moves = place no. - 1 | So , to acquire 2017th place , 1 will have to make 2016 moves. Similarly , to acquire 2016th place , 2 will have to make 2015 moves. | So, total no. of moves = 2016 + 2015 + 2014 +..... + 3 + 2 + 1 | Total no. Of moves = n(n+1)/2 .. Here n=2016 | 2016×2017/2 = 2033136
What you have shown is that using your way of "moving 1 first and then never touching it, and then moving 2 next and then never touching it, etc" will require that many steps.
Why is it the minimum ? IE You have shown that this can be achieved, buy why can't it be done with fewer steps?
To acquire 2017th place , 1 must move 2016 places because in one move , it can jump only one place. They can be more than that if comes back to previous place and then again moves to next place. Therefore , to reach 2017th place , 1 has to make 2016 moves minimum. Now , 2 is at 1st place and to reach 2016th place , it has to make 2015 moves at least.And it goes on... If I did anything wrong , please correct me , sir! @Calvin Lin
Log in to reply
I agree that you have given an approach which yields 2033136 moves to get to the desired configuration.
However, what you're missing is explaining why there is no other approach which would take fewer steps. For example, had we not swapped 1 and 2 in the very first step, then 2 only needs 2015 moves to get to the other end. So, your argument of "Similarly, to acquire 2016th place, 2 will have to make 2015 moves." might not apply to a general approach.
Log in to reply
@Calvin Lin So , would you mind telling the solution please , sir?
Log in to reply
@Toshit Jain – Think about what the crux of the steps are. E.g. we could also have obtained 2033136 by moving 2016 first, and then moving 1 next, and then moving 2015 ...
If you can find a characteristic that describes the kind of moves that we have to make, and then we can count those moves to show that we need at least 2033136 moves. (and in fact is achieved if we do not make any redundant moves like swapping 1-2 3 times at the start)
Log in to reply
@Calvin Lin – @Calvin Lin I don't really think there is a better way to 'proof' that this is the minimum. Since you have to reverse the starting positions you must move 1 n-1 positions, 2 n-2 positions, 3 n-3 positions. And so on, until the middle of the sequence. The most effective way is just push 1 to the end, than 2, than 3 and so on. If this process would take less steps, than there must be at least 2 cards in the wrong spot, since they didn't move enough positions yet. Basically when you moved 1 to the nth position, you have moved the nth card to the n-1st position. This results in starting the same process, but now with n-1 cards and 2 as the first card(since 1 is in place now).
Log in to reply
@Peter van der Linden – Yes, that's the idea. There is a simple way to express that rigorously, that doesn't assume "this is the most effective way" or "this must be the sequence of steps that we take. As I explained, try to characterize "what it means to take an effective move / what it means to waste a move".
For example, a crude / tedious approach would be to draw a graph where the 2 0 1 7 ! arrangement forms the vertices, and we connect 2 vertices with an edge if they can be obtained via a move, and then ask what is the distance between these 2 vertices. Of course, this (should) looks and sounds very unappealing to execute.
As this is a Problem of the Week, I look forward to seeing the various suggestions and proposals.
How did we jump from four numbers to 2017 numbers? Why does the pattern always hold?
Log in to reply
Yes. The number pattern is going in the sequence. for n, the result=(n-1)+(n-2)+....+1=(n-1)*n/2. The first number requires (n-1) swap after the completion of the first number, the second number from the current position requires (n-2) swap and continue the swap from the previous positions then the last number from the previous position requires 1 swap.
Log in to reply
Yes, that is a valid argument.
What remains to show is that this is indeed the minimum.
Log in to reply
@Agnishom Chattopadhyay – We solve each level in the minimum n=2, n=3, n=4,... and we came with the formula. So it is the minimum for this kind of the series.
Log in to reply
@Venkatachalam J – No, that is not a valid proof.
All you have shown is that it the pattern appears to be true, but you didn't prove that this pattern holds true for any number of cards ≥ 2 .
Log in to reply
@Pi Han Goh – It will work for counting numbers. The pattern holds true for number of cards >0. If only one card is there the number of swap=0.(n=1, Answer 0). I updated the general sequence with the counting numbers.
Let f ( n ) be number of moves it takes to swap a whole sequence into its reverse. If we add 1 extra card, then for n + 1 cards, first do f ( n ) moves to reverse the 1st n cards followed by moving the remaining card through the whole sequence. Thus for example, if we are considering 4 cards, 1 , 2 , 3 , 4 we do f ( 3 ) moves to end up with 3 , 2 , 1 , 4 then swap to get succesively 3 , 2 , 4 , 1 . 3 , 4 , 2 , 1 and finally 4 , 3 , 2 , 1 . Thus for any general n we can see that f ( n + 1 ) = f ( n ) + n
Also f ( 1 ) = 0 as for a single card it is already reversed. So solving the above relation by just summing both sides from 1 to n we see that almost all f cancel each other giving f ( n + 1 ) = f ( 1 ) + 2 ( n ) ( n + 1 ) Thus f ( n + 1 ) = 2 ( n ) ( n + 1 ) Putting n = 2 0 1 6 , we get f ( 2 0 1 7 ) = 2 0 1 7 ∗ 1 0 0 8 = 2 0 3 3 1 3 6
I still haven't figured out a way to show that this indeed is the minimum.
Log in to reply
You could look at the conversation between Toshit and Calvin in Toshit's solution
Yes, it is a worthwhile technique to try to reduce a problem to a smaller problem.
Piling then unpiling gives you 1 move and 2016 simultaneous swaps. Is that legal according to the rules?
From the beginning, we will swap 1 from the leftmost to the right most. It takes 2016 steps. Then we will swap 2 from the leftmost to the 2nd rightmost. It takes 2015 steps this time. And then we will swap 3 from the leftmost to the 3rd rightmost. It takes 2014 steps. ...... At last we swap 2016 from the leftmost to the 2016th leftmost. It takes 1 steps.
So overall it takes 1 + 2 + 3 + 4 + ... + 2016 = 2 2 0 1 6 ( 1 + 2 0 1 6 ) = 2033136
You are right, but how do we prove that this is the minimum ?
Suppose you have 2 cards then Minimum number of swaps=1 Similarly for 3 cards 3 swaps For 4 cards 6 swaps Therefore for "n" number of cards total swaps required = ( n(n-1) ) / 2 Therefore For 2017 cards swaps required = (2017 × 2016)/2 = 2017 × 1008 = 2033136
You got the right idea. But can you prove that the MINIMUM number of swaps required is 2033136? Can't this number be lowered?
Problem Loading...
Note Loading...
Set Loading...
There are many ways of achieving a solution with 2033136 swaps. Here, I will show that it is also the minimum number of swaps that is necessary. We will work with an argument based on the number of inversions of a permutation of 1, 2, ..., 2017. The initial configuration 1, 2, ..., 2017 clearly has got 0 inversions. The final configuration has got 2016 + ... + 2 + 1 = 2033136 inversions. It is already a well known fact that a single swap increments or decrements the number of inversions by exactly one. Thus, to achieve 2033136 inversions, we need at least the same number of swaps.