KatInterview

629. K Inverse Pairs Array

HardSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the number of permutations of numbers from 1 to n that have exactly k inverse pairs. An inverse pair is a pair of indices (i, j) such that i nums[j]. This is a counting problem, which often suggests a dynamic programming approach.

Solution

K Inverse Pairs Array in Python

class Solution:
    def kInversePairs(self, n: int, k: int) -> int:
        MOD = 10**9 + 7

        # dp[j] will store the number of permutations with j inverse pairs for a given i.
        # We use two lists for DP: prev_dp for i-1 and curr_dp for i.
        prev_dp = [0] * (k + 1)

        # Base case: for n = 1, there's 1 way to have 0 inverse pairs ([1]).
        prev_dp[0] = 1

        # Iterate from i = 2 to n, representing the number of elements in the permutation.
        for i in range(2, n + 1):
            curr_dp = [0] * (k + 1)
            window_sum = 0
            
            # Iterate through the number of inverse pairs j from 0 to k.
            for j in range(k + 1):
                # Add the next element to the window.
                window_sum = (window_sum + prev_dp[j]) % MOD
                
                # If the window is too large, remove the leftmost element.
                # The window size is i.
                if j >= i:
                    window_sum = (window_sum - prev_dp[j - i] + MOD) % MOD
                
                # curr_dp[j] is the sum of prev_dp[j], prev_dp[j-1], ..., prev_dp[j-i+1].
                curr_dp[j] = window_sum
            
            # Move to the next number of elements.
            prev_dp = curr_dp

        return prev_dp[k]

K Inverse Pairs Array in Java

class Solution {
    public int kInversePairs(int n, int k) {
        int MOD = 1_000_000_007;

        // dp[j] will store the number of permutations with j inverse pairs for a given i.
        // We use two arrays for DP: dp for i-1 and newDp for i.
        int[] dp = new int[k + 1];

        // Base case: for n = 1, there's 1 way to have 0 inverse pairs ([1]).
        dp[0] = 1;

        // Iterate from i = 2 to n, representing the number of elements in the permutation.
        for (int i = 2; i <= n; i++) {
            int[] newDp = new int[k + 1];
            int windowSum = 0;

            // Iterate through the number of inverse pairs j from 0 to k.
            for (int j = 0; j <= k; j++) {
                // Add the next element to the window.
                windowSum = (windowSum + dp[j]) % MOD;
                
                // If the window is too large, remove the leftmost element.
                // The window size is i.
                if (j >= i) {
                    windowSum = (windowSum - dp[j - i] + MOD) % MOD;
                }
                
                // newDp[j] is the sum of dp[j], dp[j-1], ..., dp[j-i+1].
                newDp[j] = windowSum;
            }
            // Move to the next number of elements.
            dp = newDp;
        }

        return dp[k];
    }
}

K Inverse Pairs Array in C++

class Solution {
public:
    int kInversePairs(int n, int k) {
        int MOD = 1e9 + 7;

        // dp[j] will store the number of permutations with j inverse pairs for a given i
        // We use two vectors for DP: dp for i-1 and new_dp for i
        std::vector<int> dp(k + 1, 0);

        // Base case: for n = 1, there's 1 way to have 0 inverse pairs ([1])
        dp[0] = 1;

        // Iterate from i = 2 to n, representing the number of elements in the permutation
        for (int i = 2; i <= n; ++i) {
            std::vector<int> new_dp(k + 1, 0);
            int window_sum = 0;

            // Iterate through the number of inverse pairs j from 0 to k
            for (int j = 0; j <= k; ++j) {
                // Add the next element to the window
                window_sum = (window_sum + dp[j]) % MOD;
                
                // If the window is too large, remove the leftmost element
                // The window size is i
                if (j >= i) {
                    window_sum = (window_sum - dp[j - i] + MOD) % MOD;
                }
                
                // new_dp[j] is the sum of dp[j], dp[j-1], ..., dp[j-i+1]
                new_dp[j] = window_sum;
            }
            // Move to the next number of elements
            dp = new_dp;
        }

        return dp[k];
    }
};

K Inverse Pairs Array in TypeScript

function kInversePairs(n: number, k: number): number {
    const MOD = 1_000_000_007;

    // dp[j] will store the number of permutations with j inverse pairs for a given i.
    // We use two arrays for DP: dp for i-1 and newDp for i.
    let dp = new Array(k + 1).fill(0);

    // Base case: for n = 1, there's 1 way to have 0 inverse pairs ([1]).
    dp[0] = 1;

    // Iterate from i = 2 to n, representing the number of elements in the permutation.
    for (let i = 2; i <= n; i++) {
        const newDp = new Array(k + 1).fill(0);
        let windowSum = 0;

        // Iterate through the number of inverse pairs j from 0 to k.
        for (let j = 0; j <= k; j++) {
            // Add the next element to the window.
            windowSum = (windowSum + dp[j]) % MOD;

            // If the window is too large, remove the leftmost element.
            // The window size is i.
            if (j >= i) {
                windowSum = (windowSum - dp[j - i] + MOD) % MOD;
            }

            // newDp[j] is the sum of dp[j], dp[j-1], ..., dp[j-i+1].
            newDp[j] = windowSum;
        }
        // Move to the next number of elements.
        dp = newDp;
    }

    return dp[k];
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.