# Hackerrank Weekly Challenge #5 // Problem 4

The 4th problem has strictly combinatorial nature. You're given two numbers $n$ and $k$, where $1 \leq n, k \leq 2500$. You have to answer two following questions:

1. What is the number of permutations which can be created after performing exactly $k$ swaps of adjacent elements from the sequence $1, \ldots, n$
2. What is the number of permutations which can be created after performing at most $k$ swaps of any two elements from the sequence $1, \ldots, n$

I omit in that editorial the fact that you have to return these results modulo $10^9 + 7$, but remember to do that.

### First question

Let's solve the first question first. The idea that came to my mind is to use the dynamic programming concept here. Let's define

$dp[n][k] :=$ number of permutations achievable from $1, \ldots, n$ using exactly $k$ adjacent swaps, but with the restriction that we don't swap the same elements more than once.

In other words, $dp[n][k]$ is the number of permutations of $n$ elements with exactly $k$ inversions. That observation helps us to define a recursive relation. If we have already computed $dp[n - 1][j]$ for all $j$, we can get $dp[n][k]$ by inserting the $n$-th element at some place and we know that if after that insertion, there are $m$ elements after the $n$-th element, we just created exactly $m$ new inversions. It follows that the complete recursive relations is:

$dp[n][k] = \sum\limits_{j=0}^{\min(k, n - 1)} dp[n - 1][k - j]$

which can be computed faster as:

$dp[n][k] = dp[n][k - 1] + dp[n - 1][k] - dp[n - 1][k - n]$

The last term of the above equation is define only for $k \geq n$, otherwise it's 0.

The base cases are:

$dp[n][0] = 1$ for any $n$

and

$dp[1][1] = 1$.

So far, we computed the number of permutations with exactly $k$ inversions. The last observation is that if you're allowed to do exactly $k$ swaps, you can achieve any permutation with $k, k - 2, k - 4, \ldots$ inversions (the last element of that sequence is either 0 or 1 depending on the parity of $k$), because you can erase any inversion by swapping back these elements immediately after the inversion was made with exactly one additional swap. In other words, you can use exactly 2 swaps to make no swaps.

To compute the exact result, you have to sum up the values of $dp[n][m]$ for $m \in \{k, k - 2, k - 4, \ldots, \}$.

### Second question

I suspected that the answer is somehow connected to the Stirling numbers, but didn't know how exactly. I thought that I can write a brute force solutions to provide answers for small $n, k \leq 5$ and then use the OEIS (the On-Line Encyclopedia of Integer Sequences) to figure out the exact solution. Let $f[n][k]$ be the answer to our question. It turned out that a brute force can be written as a simple DFS on the graph, where permutations are nodes, and there is an edge between two permutations if and only if you can transform one to the other using exactly one exchange of elements. I managed to do that quickly and it gave me the following table:

n / k 0 1 2 3 4
1 1
2 1 2
3 1 4 6
4 1 7 18 24
5 1 11 46 96 120

You can observe that the value for $k \geq n$ is the same as when $k = n - 1$ because if you can exchange $n - 1$ elements, you can create all permutations.

If you want to identify in OEIS an sequence depending on two variables ($n$ and $k$ here), you have to put its values in the row major form i.e. print values row by row. In that case, it's 1,1,2,1,4,6,1,7,18,24,1,11,46,96,120 and it gives the sequence A109822 and the recursive relation for that is:

$f[n][k] = \sum\limits_{i=0}^{\min(k, n)} st[n][n - i]$

where $st[n][k]$ is the absolute value of the Stirling number of the first kind for $n$ and $k$. These numbers can be computed using the recurence relation and a dynamic programming concept.

### Summary

The time complexity for both questions is $O(n \cdot k)$.

You can find my solution from the contest here: