Checking all pairs where and takes . For , that's operations.
Example: nums = [1, 3, 2, 3, 1]. Pairs to check: . Most aren't reverse pairs.
Modified merge sort: while merging two sorted halves, count pairs where left element right element. Both halves are sorted, so use two pointers. .