Count Inversions in Array

00:00
MediumSortingDivide & ConquerMerge Sort
AmazonGoogle

Count the number of inversions in an array — pairs (i,j) where i<j but arr[i]>arr[j].

Examples

Input → [2,4,1,3,5]
Output → 3
Note: pairs: 2,1,4,1,4,3
Input → [1,2,3,4,5]
Output → 0
Note: sorted
Input → [5,4,3,2,1]
Output → 10
Note: reverse sorted: n*n-1/2