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 must create 0 inversions (so it's the minimum), and total must be 2. Only [0,2,1] works: 0 inversions at position 0, and inversions (1,2) gives 1 total... wait, this needs recalculation.
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
Theater Seating with Skill-Based InversionsTheater Layout (3 seats for people with skills 0,1,2)Seat 1Seat 2Seat 3Inversion: Higher skill behind lower skillExample: [2,0,1] arrangement201Inversion 1Inversion 2DP Approach: dp[position][inversions] = ways to reach this stateโ€ข dp[0][0] = 1 (start with 0 people, 0 inversions)โ€ข When placing person with skill k at position p, creates (k - rank) new inversions๐ŸŽฏ Result: Count all valid final arrangements
Understanding the Visualization
1
Start Empty
Begin with no one seated - 1 way to have 0 inversions
2
Seat First Person
Place person with skill 0, 1, or 2 in first seat - creates different inversion potentials
3
Add Second Person
Each new person can sit ahead of or behind existing people, creating specific numbers of inversions
4
Check Constraints
At required checkpoints, eliminate arrangements that don't meet inversion requirements
5
Continue Building
Keep adding people while tracking valid arrangements until all seats are filled
Key Takeaway
๐ŸŽฏ Key Insight: Build permutations incrementally using DP, where placing element k at position p with r smaller elements already placed contributes exactly (k-r) new inversions. This allows us to efficiently count valid arrangements without generating all permutations.
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
18.6K 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