C++20 sections · 1024 units
Open in Course

Arrival - The Pattern

Counting moves

Here's what matters: you don't perform the swaps. Just calculate how many each movement needs. Moving from position ii to position 00 takes ii swaps. Moving from position jj to position n1n-1 takes n1jn-1-j swaps.

I'll show you the edge case. If the tallest soldier starts behind the shortest soldier, they pass each other during swaps. This saves one second because they swap for free while crossing.

This teaches you to simulate operations mathematically, not step by step. You count what would happen without doing it. Faster to compute and easier to code.