Problem · Array
Count Array Inversions
Learn this problemProblem statement
Given an integer array nums, return the number of inversions in it.
An inversion is a pair of indices (i, j) such that i < j and nums[i] > nums[j]. Equal values do not form an inversion.
Function
countInversions(nums: int[]) → longExamples
Example 1
nums = [2,4,1,3,5]return = 3The inverted pairs are (2,1), (4,1), and (4,3).
Example 2
nums = [5,4,3,2,1]return = 10Every pair is inverted in a length-five descending array.
Constraints
1 <= nums.length <= 200000-2^31 <= nums[i] <= 2^31 - 1- The answer fits in a signed 64-bit integer.