#K361. Count Inversion Pairs
Count Inversion Pairs
Count Inversion Pairs
Given an array of integers, count the number of inversion pairs in the array. An inversion pair is defined as a pair of indices \( (i, j) \) such that \( i a_j \). In other words, you need to count the number of pairs \( (i, j) \) where the element at index \( i \) is greater than the element at index \( j \).
The mathematical formulation is given by:
\[ \text{count} = \sum_{i=1}^{n} \sum_{j=i+1}^{n} \mathbb{1}(a_i > a_j), \]
where ( \mathbb{1}(\text{condition}) ) is 1 if the condition holds, and 0 otherwise.
Example:
- Input:
5
and5 3 4 6 2
- Output:
6
inputFormat
The input is read from standard input (stdin). The first line contains a single integer ( n ) representing the number of elements in the array. The second line contains ( n ) space-separated integers representing the array elements.
outputFormat
Output a single integer to standard output (stdout), which is the count of inversion pairs in the array.## sample
5
5 3 4 6 2
6