#K9661. Minimum Adjacent Swaps to Sort Books
Minimum Adjacent Swaps to Sort Books
Minimum Adjacent Swaps to Sort Books
You are given an array representing the heights of books on a shelf. Your task is to compute the minimum number of adjacent swaps required to sort the array in non-decreasing order.
Formally, given an integer \(n\) and an array \(h = [h_1, h_2, \dots, h_n]\), you must determine the smallest number of operations needed to reorder the array so that \(h_1 \le h_2 \le \dots \le h_n\), where in each operation you are allowed to swap two adjacent elements. This is equivalent to computing the inversion count of the array.
For example, if the input is [3, 1, 2], the minimum number of adjacent swaps required is 2.
inputFormat
The input consists of two lines:
- The first line contains a single integer \(n\), representing the number of books.
- The second line contains \(n\) space-separated integers, where the \(i\)-th integer denotes the height of the \(i\)-th book.
outputFormat
Output a single integer representing the minimum number of adjacent swaps required to sort the given array.
## sample3
3 1 2
2
</p>