Skip to interview prep
Apexed
Problem list116 / 125

116. Count Inversions

HardDivide & conquer

Return the number of pairs (i, j) with i < j and nums[i] > nums[j]. Aim for O(n log n).

Example 1

Input: nums = [2, 4, 1, 3, 5]
Output: 3
Explanation: (2,1), (4,1), (4,3)

Constraints

  • 1 ≤ nums.length ≤ 10⁵

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 5 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Count Inversions — coding interview practice — Apexed