Given a series of n numbers, I need an algorithm that runs in worst case O(n*k) to figure out how many arrangements of those n numbers will give me a score of exactly k.
Note that the series does not contain duplicate elements.
The score of a series of numbers is calculated by the number of smaller numbers an element in the series has before it.
For example, The score of the below series would be:
series = [5,3,6]
5 has no smaller number before it, so 0
3 has no smaller number before it, so 0
6 has 2 smaller numbers before it (5,3) so 2
Adding all of this, we get a total score for the series as 2.
What I have tried to do:
I have tried to find all possible arrangements of the series -> (n! many arrangements)
and count the ones that have a score of k.
But this has a worst case time complexity of O(n!). Any help/ideas will be much appreciated. Thanks!