Count Inversions

Given an integer array nums, return the number of inversions in the array, where an inversion is a pair (i, j) such that i < j and nums[i] > nums[j]. This is a standard Fenwick Tree application after coordinate compression.

Input Format

nums = integer array

Output Format

total number of inversions

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9

Examples

Example 1:

Input:

nums = [8,4,2,1]

Output:

6

Explanation:

Every earlier element is larger than every later element, so all 6 pairs are inversions.

Example 2:

Input:

nums = [3,1,2]

Output:

2

Explanation:

The inversions are (3,1) and (3,2).

Loading...
Count Inversions - Advanced Trees DSA Problem