Arrival of the General

Practice this coding question on Coding Gurukul.

Problem statement

A parade coordinator arranges a line of students before a ceremonial inspection. The front position must contain someone of maximum height, and the final position must contain someone of minimum height. All other positions are unrestricted. Students can change places only by swapping with an immediate neighbour, so the coordinator wants the smallest number of adjacent swaps needed. Your task Given the heights in their current order, find the minimum adjacent-swap count required to put a tallest student first and a shortest student last. Heights may repeat, and you may choose any occurrence of a maximum or minimum height to minimise the work. Important details A swap can move both selected students closer to their destinations. Repeated extreme heights should not force unnecessary movement. If every height is equal, the line already satisfies both conditions. Worked example For [2,1,3], move 3 left twice to get [3,2,1]. The shortest student is then already last, so the answer is 2.

Input format

First line: n. Second line: n heights.

Output format

Print the minimum number of adjacent swaps.

Constraints

2 <= n <= 100; heights are integers from 1 to 100.

Sample input

3
2 1 3

Sample output

2

Sign in to run or submit a solution in C++, Java or Python.