Count the Number of Inversions - Problem

Count the Number of Inversions

You're given an integer n and a 2D array requirements, where each requirements[i] = [end_i, cnt_i] represents a constraint on the permutation.

What is an inversion?
In an array, an inversion is a pair of indices (i, j) where i < j but nums[i] > nums[j]. It's essentially when a larger element appears before a smaller one.

Your Goal:
Find how many permutations of [0, 1, 2, ..., n-1] satisfy ALL the given requirements. For each requirement [end_i, cnt_i], the subarray perm[0..end_i] must have exactly cnt_i inversions.

Since the answer can be very large, return it modulo 109 + 7.

Example: If n=3 and requirements=[[2,2]], we need permutations where the first 3 elements have exactly 2 inversions. The permutation [2,1,0] works because it has inversions: (0,1), (0,2), (1,2) - but wait, that's 3 inversions, not 2!

Input & Output

example_1.py — Basic Case
$ Input: n = 3, requirements = [[2,2]]
Output: 2
💡 Note: We need permutations of [0,1,2] where the first 3 elements have exactly 2 inversions. Valid permutations: [2,0,1] has inversions (0,1), (0,2) = 2 total. [1,2,0] has inversions (0,2), (1,2) = 2 total.
example_2.py — Multiple Requirements
$ Input: n = 3, requirements = [[0,0], [2,2]]
Output: 1
💡 Note: First element (arr[0]) must create 0 inversions with itself (which is always true), and the entire array must have exactly 2 inversions. The permutation [0,2,1] satisfies both: arr[0..0] has 0 inversions, and arr[0..2] has 1 inversion at (1,2).
example_3.py — Impossible Case
$ Input: n = 2, requirements = [[0,1]]
Output: 0
💡 Note: With only 1 element at position 0, it's impossible to have 1 inversion since inversions need at least 2 elements.

Constraints

  • 2 ≤ n ≤ 300
  • 0 ≤ requirements.length ≤ n
  • requirements[i] = [endi, cnti]
  • 0 ≤ endi ≤ n - 1
  • 0 ≤ cnti ≤ 400
  • All endi are distinct

Visualization

Tap to expand
Count the Number of Inversions INPUT Permutation elements: [0, 1, 2] n = 3 0 1 2 Requirements: [[2, 2]] end=2, cnt=2 perm[0..2] must have exactly 2 inversions What is an inversion? i < j but nums[i] > nums[j] Example: [2,0,1] (0,1): 2>0, (0,2): 2>1 = 2 inversions ALGORITHM STEPS 1 Define DP State dp[i][j] = permutations of [0..i] with j inversions 2 Base Case dp[0][0] = 1 (empty has 0 inversions) 3 Transition Add element at position k creates k new inversions 4 Check Requirements Only count states that satisfy all constraints DP Transition Table i\j 0 1 2 0 1 0 0 1 1 1 0 2 1 2 2 FINAL RESULT Valid Permutations: [2, 1, 0] 3 inversions at end=2 [2, 0, 1] 2 inversions [OK] [1, 2, 0] 2 inversions [OK] Output: 2 2 permutations satisfy requirement [[2,2]] (mod 10^9 + 7) Key Insight: The DP approach counts permutations by building them element by element. When placing element i at position k (from the right), it creates exactly k new inversions. Requirements constrain which inversion counts are valid at each prefix length. Time complexity: O(n^2 * maxInversions). TutorialsPoint - Count the Number of Inversions | DP Approach
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
18.7K Views
Medium Frequency
~35 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen