Given an array, an inversion is defined as a pair a[i], a[j] such that a[i] > a[j] and i < j. We are given two numbers N and k, we need to tell how many permutation of first N number have exactly K inversion.
Input : N = 3, K = 1 Output : 2 Explanation : Total Permutation of first N number, 123, 132, 213, 231, 312, 321 Permutation with 1 inversion : 132 and 213 Input : N = 4, K = 2 Output : 5
A Naïve way to solve this problem is noting down all permutation then checking count of inversion in them but iterating through permutation itself will take O(N!) time, which is too large.
We can solve this problem using dynamic programming approach. Below is recursive formula.
If N is 0, Count(0, K) = 0 If K is 0, Count(N, 0) = 1 (Only sorted array) In general case, If we have N number and require K inversion, Count(N, K) = Count(N - 1, K) + Count(N – 1, K - 1) + Count(N – 1, K – 2) + .... + Count(N – 1, 0)
How does above recursive formula work?
If we have N number and want to have K permutation and suppose all permutation of (N – 1) number are written somewhere, the new number (Nth number and largest) need to be placed in all permutation of (N – 1) number and those whose inversion count becomes K after adding this number should be added in our answer. Now take those set of permutation of (N – 1) number which has let (K – 3) inversion, now we can place this new largest number at position 3 from last, then inversion count will be K, so count(N – 1, K – 3) should be added to our answer, same argument can be given for other inversion also and we will reach to above recursion as final answer.
Below code is written following above recursion in memorization way.
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Minimum number of given operations required to convert a permutation into an identity permutation
- Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K
- Check if any permutation of a large number is divisible by 8
- Number of moves required to guess a permutation.
- Number of valid indices in the permutation of first N natural numbers
- Counting Inversions using Set in C++ STL
- Subarray Inversions
- Significant Inversions in an Array
- Count inversions in an array | Set 3 (Using BIT)
- Counting inversions in all subarrays of given size
- Count Inversions of size three in a given array
- Count inversions in an array | Set 2 (Using Self-Balancing BST)
- Count inversions of size k in a given array
- Time complexity of insertion sort when there are O(n) inversions?
- Count Inversions in an array | Set 1 (Using Merge Sort)
- Counting inversions in an array using segment tree
- Count number of permutation of an Array having no SubArray of size two or more from original Array
- Permutation Coefficient
- K difference permutation