C++20 sections · 1024 units
Open in Course

Problem - Arrival of the General

Finding positions

You have nn soldiers standing in a row, each with a specific height. You need to move the tallest soldier to the front and the shortest soldier to the back. Each swap with an adjacent soldier takes one second.

Your task: calculate the minimum time needed. This tests your understanding of finding minimum and maximum elements, plus counting swaps. The STL provides functions to find positions directly.

Before reading on, think: how many swaps does it take to move an element from position ii to position 00? What if you need to move two elements in opposite directions?