C++20 sections · 1024 units
Open in Course

Arrival - Implementation

Complete solution


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 O(n)O(n) where nn is the number of soldiers.