K Inverse Pairs Array

HardDynamic Programming2d dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

An inverse pair in an array is a pair of indices (i, j) with i < j and a[i] > a[j]. Given n and k, count how many permutations of 1, 2, ..., n contain exactly k inverse pairs. Return the count modulo 10^9 + 7. If no permutation qualifies, return 0. Aim for O(n * k) time.

Examples

Input: n = 3, k = 0
Output: 1
Only [1,2,3] has no inverse pairs.
Input: n = 3, k = 1
Output: 2
[1,3,2] and [2,1,3] each have exactly one inverse pair.

Constraints

  • 1 <= n <= 1000
  • 0 <= k <= 1000

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More 2d dp problems