C++20 sections · 1024 units
Open in Course

Problem - Arrival of the General

Rearranging soldiers

You're observing nn soldiers standing in a row, each with a different height. The general wants the tallest soldier at the front and the shortest at the back. You can swap adjacent soldiers, and each swap takes 11 second.

Your task: find the minimum time to arrange the soldiers correctly. This tests your ability to track both values and their positions using pairs. Before reading further, think: if the tallest soldier is at position 55 and the shortest at position 22 in a line of 77 soldiers, how many swaps do you need?