plaintext
function arrival_general(heights)
n := size of heights
max_pos := index of max_element(heights)
min_pos := index of min_element(heights)
swaps := max_pos + (n - 1 - min_pos)
if max_pos > min_pos then
swaps := swaps - 1
return swaps
The max_element() and min_element() functions return iterators pointing to the elements. The time complexity is where is the number of soldiers.