CSES Solutions - Permutation Inversions Last Updated : 06 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Your task is to count the number of permutations of 1, 2, … n that have exactly k inversions (i.e., pairs of elements in the wrong order). Examples: Input: n = 4, k = 3Output: 6Explanation: For n=4 and k=3, there are 6 permutations that satisfy the given conditions. The permutations are: [1, 4, 3, 2][2, 3, 4, 1][2, 4, 1, 3][3, 1, 4, 2][3, 2, 1, 4][4, 1, 2, 3]Input: n = 5, k = 2Output: 5Explanation: For n=5 and k=2, there are 5 permutations that satisfy the given conditions. Approach: We can solve this problem using dynamic programming. We'll define a 2D array dp[i][j] to represent the number of the permutations of the first i integers that have exactly j inversions. We can calculate dp[i][j] using the following recurrence relation: dp[i][j] = ⅀dp[i - 1][j - x] for all x from 0 to i - 1 This recurrence relation represents the fact that to the form a permutation of the first i integers with the j inversions we can fix the position of the integer i and count the number of the permutations of the first i−1 integers with j−x inversions where x is the number of the inversions involving i. Below is the implementation of the following approach: C++ #include <iostream> #include <vector> using namespace std; const int MOD = 1e9 + 7; int main() { // Sample Input int n = 4, k = 3; vector<int> dp(k + 1, 0); dp[0] = 1; // Iterate through the integers from the 1 to n for (int i = 1; i <= n; ++i) { vector<int> new_dp(k + 1, 0); // Iterate through the possible number of the // inversions from 0 to k for (int j = 0; j <= k; ++j) { // Compute the prefix sum new_dp[j] = (new_dp[j] + dp[j]) % MOD; if (j > 0) { // Compute the suffix sum new_dp[j] = (new_dp[j] + new_dp[j - 1]) % MOD; } // Subtract the prefix sum from suffix sum if // needed if (j - i >= 0) { new_dp[j] = (new_dp[j] - dp[j - i] + MOD) % MOD; } } dp = new_dp; } cout << dp[k] << endl; return 0; } Java public class Main { static final int MOD = 1000000007; public static void main(String[] args) { int n = 4, k = 3; int[] dp = new int[k + 1]; dp[0] = 1; for (int i = 1; i <= n; ++i) { int[] newDp = new int[k + 1]; for (int j = 0; j <= k; ++j) { newDp[j] = (newDp[j] + dp[j]) % MOD; if (j > 0) { newDp[j] = (newDp[j] + newDp[j - 1]) % MOD; } if (j - i >= 0) { newDp[j] = (newDp[j] - dp[j - i] + MOD) % MOD; } } dp = newDp; } System.out.println(dp[k]); } } Python MOD = 1000000007 def main(): n = 4 k = 3 dp = [0] * (k + 1) dp[0] = 1 for i in range(1, n + 1): new_dp = [0] * (k + 1) for j in range(k + 1): new_dp[j] = (new_dp[j] + dp[j]) % MOD if j > 0: new_dp[j] = (new_dp[j] + new_dp[j - 1]) % MOD if j - i >= 0: new_dp[j] = (new_dp[j] - dp[j - i] + MOD) % MOD dp = new_dp print(dp[k]) if __name__ == "__main__": main() JavaScript const MOD = 1e9 + 7; // Sample Input let n = 4, k = 3; let dp = new Array(k + 1).fill(0); dp[0] = 1; // Iterate through the integers from the 1 to n for (let i = 1; i <= n; ++i) { let new_dp = new Array(k + 1).fill(0); // Iterate through the possible number of the inversions from 0 to k for (let j = 0; j <= k; ++j) { // Compute the prefix sum new_dp[j] = (new_dp[j] + dp[j]) % MOD; if (j > 0) { // Compute the suffix sum new_dp[j] = (new_dp[j] + new_dp[j - 1]) % MOD; } // Subtract the prefix sum from the suffix sum if needed if (j - i >= 0) { new_dp[j] = (new_dp[j] - dp[j - i] + MOD) % MOD; } } dp = new_dp; } console.log(dp[k]); Output6 Time Complexity: O(n * k)Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article Number of Transpositions in a Permutation S subramanyasmgm Follow Improve Article Tags : Dynamic Programming DSA Practice Tags : Dynamic Programming Similar Reads Number of Transpositions in a Permutation Cycle notation is a compact way to represent a permutation by breaking it down into cycles. A cycle represents a set of elements that are permuted (or swapped) among each other.Examples:Let us consider the permutation p = [5, 1, 4, 2, 3] of [1, 2, 3, 4, 5], the elements are moved as 1 â 5, 5 â 3, 3 6 min read Number of permutation with K inversions | Set 2 Given two integers N and K, the task is to count the number of permutations of the first N natural numbers having exactly K inversions. Since the count can be very large, print it modulo 109 + 7. An inversion is defined as a pair a[i], a[j] such that a[i] > a[j] and i < j. Examples: Input: N = 8 min read Permutations of given String Given a string s, the task is to return all permutations of a given string in lexicographically sorted order.Note: A permutation is the rearrangement of all the elements of a string. Duplicate arrangement can exist.Examples:Input: s = "ABC"Output: "ABC", "ACB", "BAC", "BCA", "CAB", "CBA"Input: s = " 5 min read Inverse Permutation Given an array of size n of integers in range from 1 to n, we need to find the inverse permutation of that array. An inverse permutation is a permutation which you will get by inserting position of an element at the position specified by the element value in the array. For better understanding, cons 9 min read Number of permutation with K inversions We are given two numbers n and k, the task is to find how many permutations of the first n number have exactly k inversion. Two elements in a sequence form an inversion if the smaller element appears before the larger element.Examples: Input: n = 3, k = 1Output: 2Explanation: Total Permutation of fi 15 min read All permutations of a string using iteration A permutation, also called an âarrangement numberâ or âorderâ, is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation ( Source: Mathword ) Below are the permutations of string ABC. ABC ACB BAC BCA CBA CAB We hav 4 min read Like