> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# K Inverse Pairs Array Python Solution

> Tested Python solution for LeetCode 629 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 629, [Hard](/catalog/hard). Topics: [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/k-inverse-pairs-array/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 629   # by problem number
lcpy gen -s k_inverse_pairs_array   # by problem name
```

## Problem

For an integer array `nums`, an **inverse pair** is a pair of integers `[i, j]` where `0 <= i < j < nums.length` and `nums[i] > nums[j]`.

Given two integers `n` and `k`, return the number of different arrays consisting of numbers from `1` to `n` such that there are exactly `k` **inverse pairs**. Since the answer can be huge, return it **modulo** `10^9 + 7`.

### Examples

```
Input: n = 3, k = 0
Output: 1
Explanation: Only the array [1,2,3] which consists of numbers from 1 to 3 has exactly 0 inverse pairs.
```

```
Input: n = 3, k = 1
Output: 2
Explanation: The array [1,3,2] and [2,1,3] have exactly 1 inverse pair.
```

### Constraints

* 1 \<= n \<= 1000
* 0 \<= k \<= 1000

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/k_inverse_pairs_array/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/k_inverse_pairs_array/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * k)
    # Space: O(k)
    def k_inverse_pairs(self, n: int, k: int) -> int:
        mod = 1_000_000_007
        # dp[j]: arrays using values 1..i with exactly j inverse pairs.
        dp = [0] * (k + 1)
        dp[0] = 1
        for i in range(2, n + 1):
            prefix = [0] * (k + 2)
            for j in range(k + 1):
                prefix[j + 1] = (prefix[j] + dp[j]) % mod
            # Inserting value i adds between 0 and i-1 new inverse pairs.
            new = [0] * (k + 1)
            for j in range(k + 1):
                low = max(0, j - (i - 1))
                new[j] = (prefix[j + 1] - prefix[low]) % mod
            dp = new
        return dp[k]
```

## Complexity

| Time | Space |
| - | - |
| O(n \* k) | O(k) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.