TITLE: Number of permutations such that adjacent elements don't differ by more than $K$
QUESTION [7 upvotes]: Given $N$ and $K$, I need to count number of permutations of $1, 2, 3,\ldots, N$ in which no adjacent elements differ by more than $K$. How do I approach this problem?

REPLY [2 votes]: This is a question that appeared in a contest i.e Codecraft 2015 organised by IIITH and its editorial is available here. You can also submit your code in the gym.
Basically you require DP + Bitmask.You need to keep track of last_used element and all other elements included so far.You can do it using recursion and it will require memoization to optimize it.