Problem of the day
There is an integer array ‘A’ of size ‘N’.
Number of inversions in an array can be defined as the number of pairs of ‘i’, ‘j’ such that ‘i’ < ‘j’ and ‘A[i]’ > ‘A[j]’.
You must return the number of inversions in the array.
Input:
A = [5, 3, 2, 1, 4], N = 5
Output:
7
Explanation:
The pairs satisfying the condition for inversion are (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), and (3, 4).
The number of inversions in the array is 7.
The first line contains an integer, ‘N’.
The second line has ‘N’ integers denoting the array ‘A’.
Output Format:-
You must return the number of inversions in the array.
Note:-
You don’t need to print anything. Just implement the given function.
4
4 3 2 1
6
Input:
A = [4, 3, 2, 1], N = 4
Output:
6
Explanation:
The pairs satisfying the condition for inversion are (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), and (3, 4).
The number of inversions in the array is 6.
5
1 20 6 4 5
5
1 <= N <= 10^5
1 <= A[i] <= 10^9
Time Limit: 1 sec