An inversion is a pair of positions i < j where nums[i] > nums[j]: two values that are the wrong way round. Count them.
The starter code checks every pair. It is correct but too slow for long lists. Make it fast.
Example 1
Input:
count_inversions([3, 1, 2])
Output:
2
The inversions are (3, 1) and (3, 2).
Example 2
Input:
count_inversions([1, 2, 3])
Output:
0
0 <= len(nums) <= 5 * 10**4-10**9 <= nums[i] <= 10**91.25 * 10**9.No results yet
Run your code to check it against the examples, or submit to run every test.