Rearrangements of 1..m With a Fixed Number of Out-of-Order Pairs
Dynamic ProgrammingAuto-graded · 28 test casesLast updated
Count arrangements of 1..m with exactly n out-of-order pairs, modulo 10^9+7, using a DP over insertion positions optimized with a sliding-window sum.
You are given two integers m and n.
Think about every ordering of the numbers 1, 2, ..., m, where each number appears exactly once. In an ordering p (0-indexed), a pair of positions (i, j) is out of order if i < j and p[i] > p[j].
Count how many orderings of 1..m contain exactly n out-of-order pairs. Because the count can be huge, return it modulo 10^9 + 7.
Details
- An ordering with no out-of-order pairs is the sorted one,
[1, 2, ..., m]. - If
nis larger than the number of pairs that can possibly be out of order, the answer is0. - Only the count modulo
10^9 + 7is returned, as a non-negative integer in[0, 10^9 + 6].
Implement count_orderings(m, n) returning that count.
Constraints
1 <= m <= 10000 <= n <= 1000
Examples
Example 1
Input: count_orderings(3, 0)
Output: 1
Example 2
Input: count_orderings(3, 1)
Output: 2
Example 3
Input: count_orderings(4, 3)
Output: 6
Example 4
Input: count_orderings(1, 0)
Output: 1
Hints
Hint 1
Think about building the ordering by inserting the values 1, 2, ..., m one at a time. What happens to the out-of-order pair count when you insert the current largest value?
Hint 2
If the new largest value is placed so that k elements come after it, it adds exactly k out-of-order pairs, for any k from 0 to (current size - 1). This gives dp[i][j] as a sum of up to i entries from the previous row.
Hint 3
That sum is a window of width i over the previous row. Maintain it as a running sum: add prev[j], and once j >= i subtract prev[j - i]. Keep everything modulo 10^9+7 and only store two rows.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Build the orderings by growing the set of values one at a time. Take any ordering of 1..i-1 and insert the value i. Since i is bigger than everything already present, it forms an out-of-order pair with exactly those elements that end up after it. If k elements follow it, it adds exactly k new pairs, and k can be anything from 0 to i-1. The pairs among the older elements are untouched.
Let dp[i][j] be the number of orderings of 1..i with exactly j out-of-order pairs. Then
dp[i][j] = dp[i-1][j] + dp[i-1][j-1] + ... + dp[i-1][j-(i-1)]
(terms with a negative index are treated as 0). Summing directly costs O(m * n * m), which is too slow. But the right-hand side is a window of width i over the previous row, so it can be maintained as a running sum: moving from j-1 to j adds dp[i-1][j] and, when j >= i, removes dp[i-1][j-i].
Why it is correct
- Each ordering of
1..iarises from exactly one ordering of1..i-1(removei) together with one choice ofk(how many elements followi), so nothing is double counted or missed. - The added pair count is exactly
k, so the transition is exactly the window sum above. - Starting from the empty ordering (
dp[0] = 1at 0 pairs) and applyingmsteps gives the correct row. Ifnexceedsm(m-1)/2, no state reachesn, so the result is naturally0. - Python's
%always returns a non-negative value, so subtraction under the modulus is safe.
Complexity
- Time: O(m * n), about 10^6 simple operations at the limits.
- Space: O(n), since only the previous row is needed.
Reference solution
MOD = 1000000007
def count_orderings(m: int, n: int) -> int:
dp = [0] * (n + 1)
dp[0] = 1
for size in range(1, m + 1):
next_dp = [0] * (n + 1)
window = 0
for j in range(n + 1):
window = (window + dp[j]) % MOD
if j >= size:
window = (window - dp[j - size]) % MOD
next_dp[j] = window
dp = next_dp
return dp[n]