#C2472. Minimum Adjacent Swaps to Sort Array

    ID: 45792 Type: Default 1000ms 256MiB

Minimum Adjacent Swaps to Sort Array

Minimum Adjacent Swaps to Sort Array

Given an array of integers, your task is to sort the array in ascending order using adjacent swaps. At the same time, determine the minimum number of adjacent swaps required to achieve the sorted order.

You can think of the problem as follows: given an array \( A \) of size \( N \), perform adjacent swaps until the array is sorted. The total number of swaps performed is the answer. Note that each swap only exchanges two consecutive elements in the array.

For example, if \( A = [3, 1, 5, 2, 4] \), the output should be the sorted array \( [1,2,3,4,5] \) and the number of swaps required, which is 4.

inputFormat

The first line contains an integer \( N \) denoting the number of elements in the array.

The second line contains \( N \) space-separated integers representing the elements of the array.

outputFormat

Output two lines:

  1. The first line should print the sorted array elements separated by a single space.
  2. The second line should print a single integer, which is the minimum number of adjacent swaps required.
## sample
5
3 1 5 2 4
1 2 3 4 5

4

</p>