Number of permutation with K inversions
Last Updated :
23 Jul, 2025
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 = 1
Output: 2
Explanation: Total Permutation of first 3 numbers are 123, 132, 213, 231, 312, 321
Permutation with 1 inversion: 132 and 213
Input: n = 3, k = 3
Output: 1
Explanation: Permutation with 3 inversions: 321
Using Recursion - O(n! * k) Time and O(n) Space
Base Cases:
- If n == 0, no permutations are possible, so return 0.
- If k == 0, there is exactly one permutation (the sorted order), so return 1.
Recursive Relation:
- For each possible position i where the nth element can be placed (where i ranges from 0 to min(k, n-1)), we recursively call the function to calculate the number of permutations of n-1 elements with k-i inversions. This represents placing the
n
th element in a way that creates i
inversions.
countPermWithkInversions(n, k) = Σ(countPermWithkInversions(n-1, k-i)) for i = 0 to min(k, n-1)
C++
// C++ code to count permutations with exactly
// k inversions using Recursion
#include <iostream>
using namespace std;
int countPermWithkInversions(int n, int k) {
// Base cases
// no permutations possible with 0 elements
if (n == 0) return 0;
// Only one way to have 0 inversions: sorted order
if (k == 0) return 1;
// Initialize result for this recursive step
int result = 0;
// Recursively sum up all valid counts
// by placing the nth largest element
// in positions that create the
// required inversion counts
for (int i = 0; i <= min(k, n - 1); i++) {
result += countPermWithkInversions(n - 1, k - i);
}
return result;
}
int main() {
int n = 4;
int k = 2;
cout << countPermWithkInversions(n, k);
return 0;
}
Java
// Java code to count permutations with
// exactly k inversions using Recursion
class GfG {
static int countPermWithkInversions(int n, int k) {
// Base cases
// no permutations possible with 0 elements
if (n == 0) return 0;
// Only one way to have 0 inversions: sorted order
if (k == 0) return 1;
// Initialize result for this recursive step
int result = 0;
// Recursively sum up all valid counts
// by placing the nth largest element
// in positions that create the
// required inversion counts
for (int i = 0; i <= Math.min(k, n - 1); i++) {
result += countPermWithkInversions(n - 1, k - i);
}
return result;
}
public static void main(String[] args) {
int n = 4;
int k = 2;
System.out.println(countPermWithkInversions(n, k));
}
}
Python
# Python code to count permutations with exactly
# k inversions using Recursion
def countPermWithkInversions(n, k):
# Base cases
# no permutations possible with 0 elements
if n == 0:
return 0
# Only one way to have 0 inversions: sorted order
if k == 0:
return 1
# Initialize result for this recursive step
result = 0
# Recursively sum up all valid counts
# by placing the nth largest element
# in positions that create the
# required inversion counts
for i in range(min(k, n - 1) + 1):
result += countPermWithkInversions(n - 1, k - i)
return result
if __name__ == "__main__":
n = 4
k = 2
print(countPermWithkInversions(n, k))
C#
// C# code to count permutations with exactly
// k inversions using Recursion
using System;
class GfG {
static int countPermWithkInversions(int n, int k) {
// Base cases
// no permutations possible with 0 elements
if (n == 0) return 0;
// Only one way to have 0 inversions: sorted order
if (k == 0) return 1;
// Initialize result for this recursive step
int result = 0;
// Recursively sum up all valid counts
// by placing the nth largest element
// in positions that create the
// required inversion counts
for (int i = 0; i <= Math.Min(k, n - 1); i++) {
result += countPermWithkInversions(n - 1, k - i);
}
return result;
}
static void Main() {
int n = 4;
int k = 2;
Console.WriteLine(countPermWithkInversions(n, k));
}
}
JavaScript
// Javascript code to count permutations with
// exactly k inversions using Recursion
function countPermWithkInversions(n, k) {
// Base cases
// no permutations possible with 0 elements
if (n === 0) return 0;
// Only one way to have 0 inversions: sorted order
if (k === 0) return 1;
// Initialize result for this recursive step
let result = 0;
// Recursively sum up all valid counts
// by placing the nth largest element
// in positions that create the
// required inversion counts
for (let i = 0; i <= Math.min(k, n - 1); i++) {
result += countPermWithkInversions(n - 1, k - i);
}
return result;
}
const n = 4;
const k = 2;
console.log(countPermWithkInversions(n, k));
Using Top-Down DP (Memoization) – O(n*k*k) Time and O(n*k) Space
The above recursive solution satisfies two key properties of Dynamic Programming, enabling us to use memoization for an optimized approach.
1. Optimal Substructure
We solve the problem of counting permutations with exactly k inversions by breaking it into subproblems involving fewer elements and inversions. Specifically, if we have n elements and k required inversions, we can compute it by trying positions for the largest element in a way that contributes to the inversion count.
The recursive relation is:
- countPermWithkInversions(n, k) = Σ countPermWithkInversions(n-1, k-i)
Here, we consider placing the largest element in positions that create i inversions, where i ranges from 0 to min(k, n-1).
2. Overlapping Subproblems:
Many subproblems are computed multiple times when finding permutations for smaller subsets and inversion counts. For example, while calculating the result for (n, k), subproblems for (n-1, k-i) are encountered repeatedly.
We use a 2D array memo of size (n+1) x (k+1), initialized to -1, to store results of subproblems. If a result is already stored in memo[n][k], we return it directly, avoiding redundant calculations.
C++
// C++ code to find number of permutation
// with k inversion using Memoization
#include <bits/stdc++.h>
using namespace std;
// Helper function for recursive computation
int kInversionsHelper(int n, int k,
vector<vector<int>>& memo) {
// Base case: If there are no elements,
// no permutations are possible
if (n == 0) return 0;
// Base case: If k is 0, only one way to
// have 0 inversions: sorted order
if (k == 0) return 1;
// Check if the result is already
// computed (memoization)
if (memo[n][k] != -1) return memo[n][k];
int result = 0;
// Loop through possible inversion counts
// for the nth largest element
for (int i = 0; i <= min(k, n - 1); i++) {
// Recursively count permutations for the
// remaining elements and inversions
result = (result +
kInversionsHelper(n - 1, k - i, memo));
}
return memo[n][k] = result;
}
// Function to count permutations with k inversions
int countPermWithkInversions(int n, int k) {
// Initialize memoization table with -1
// to indicate uncomputed values
vector<vector<int>> memo(n + 1, vector<int>(k + 1, -1));
return kInversionsHelper(n, k, memo);
}
int main() {
int n = 4;
int k = 2;
cout << countPermWithkInversions(n, k);
return 0;
}
Java
// Java code to find number of permutation
// with k inversions using Memoization
import java.util.Arrays;
class GfG {
// Helper function for recursive computation
static int kInversionsHelper(int n, int k, int[][] memo) {
// Base case: If there are no elements,
// no permutations are possible
if (n == 0) return 0;
// Base case: If k is 0, only one way to
// have 0 inversions: sorted order
if (k == 0) return 1;
// Check if the result is already
// computed (memoization)
if (memo[n][k] != -1) return memo[n][k];
int result = 0;
// Loop through possible inversion counts
// for the nth largest element
for (int i = 0; i <= Math.min(k, n - 1); i++) {
// Recursively count permutations for the
// remaining elements and inversions
result = (result
+ kInversionsHelper(n - 1, k - i, memo));
}
return memo[n][k] = result;
}
// Function to count permutations with k inversions
static int countPermWithkInversions(int n, int k) {
// Initialize memoization table with -1
// to indicate uncomputed values
int[][] memo = new int[n + 1][k + 1];
for (int[] row : memo) {
Arrays.fill(row, -1);
}
return kInversionsHelper(n, k, memo);
}
public static void main(String[] args) {
int n = 4;
int k = 2;
System.out.println(countPermWithkInversions(n, k));
}
}
Python
# Python code to find number of permutation
# with k inversions using Memoization
def kInversionsHelper(n, k, memo):
# Base case: If there are no elements,
# no permutations are possible
if n == 0:
return 0
# Base case: If k is 0, only one way to
# have 0 inversions: sorted order
if k == 0:
return 1
# Check if the result is already
# computed (memoization)
if memo[n][k] != -1:
return memo[n][k]
result = 0
# Loop through possible inversion counts
# for the nth largest element
for i in range(min(k, n - 1) + 1):
# Recursively count permutations for the
# remaining elements and inversions
result = (result + kInversionsHelper(n - 1, k - i, memo))
memo[n][k] = result
return result
def countPermWithkInversions(n, k):
# Initialize memoization table with -1
# to indicate uncomputed values
memo = [[-1 for _ in range(k + 1)] for _ in range(n + 1)]
return kInversionsHelper(n, k, memo)
if __name__ == "__main__":
n = 4
k = 2
print(countPermWithkInversions(n, k))
C#
// C# code to find number of permutation
// with k inversions using Memoization
using System;
class GfG {
// Helper function for recursive computation
static int kInversionsHelper(int n, int k, int[,] memo) {
// Base case: If there are no elements,
// no permutations are possible
if (n == 0) return 0;
// Base case: If k is 0, only one way to
// have 0 inversions: sorted order
if (k == 0) return 1;
// Check if the result is already
// computed (memoization)
if (memo[n, k] != -1) return memo[n, k];
int result = 0;
// Loop through possible inversion counts
// for the nth largest element
for (int i = 0; i <= Math.Min(k, n - 1); i++) {
// Recursively count permutations for the
// remaining elements and inversions
result = (result + kInversionsHelper(n - 1, k - i, memo));
}
return memo[n, k] = result;
}
static int countPermWithkInversions(int n, int k) {
// Initialize memoization table with -1
// to indicate uncomputed values
int[,] memo = new int[n + 1, k + 1];
for (int i = 0; i < memo.GetLength(0); i++) {
for (int j = 0; j < memo.GetLength(1); j++) {
memo[i, j] = -1;
}
}
return kInversionsHelper(n, k, memo);
}
static void Main() {
int n = 4;
int k = 2;
Console.WriteLine(countPermWithkInversions(n, k));
}
}
JavaScript
// JavaScript code to find number of permutation
// with k inversions using Memoization
function kInversionsHelper(n, k, memo) {
// Base case: If there are no elements,
// no permutations are possible
if (n === 0)
return 0;
// Base case: If k is 0, only one way to
// have 0 inversions: sorted order
if (k === 0)
return 1;
// Check if the result is already
// computed (memoization)
if (memo[n][k] !== -1)
return memo[n][k];
let result = 0;
// Loop through possible inversion counts
// for the nth largest element
for (let i = 0; i <= Math.min(k, n - 1); i++) {
// Recursively count permutations for the
// remaining elements and inversions
result = (result
+ kInversionsHelper(n - 1, k - i, memo));
}
memo[n][k] = result;
return result;
}
function countPermWithkInversions(n, k) {
// Initialize memoization table with -1
// to indicate uncomputed values
const memo = Array.from({length : n + 1},
() => Array(k + 1).fill(-1));
return kInversionsHelper(n, k, memo);
}
const n = 4;
const k = 2;
console.log(countPermWithkInversions(n, k));
Using Bottom-Up DP (Tabulation) – O(n*k*k) Time and O(n*k) Space
We create a 2D array dp of size (n + 1)*(k + 1), where the state dp[l][r] represents the number of permutations of l elements with exactly r inversions.
Base Case: dp[l][0] = 1 for all l, since there's only one way to have zero inversions-by arranging elements in sorted order.
For each element count l from 1 to n:
- For each inversion count r from 1 to k, calculate the number of valid permutations.
- To compute dp[l][r], we iterate over all possible positions of the largest element, which can create up to min(r, l-1) inversions, summing up the results from previous states
This relation fills the DP table by counting valid permutations based on subproblems:
- dp[l][r] = sum of dp[l-1][r-i] for all valid i.
The final result is stored in dp[n][k], representing the total permutations of n elements with exactly k inversions.
C++
// C++ code to find number of permutation
// with K inversions using Tabulation
#include <bits/stdc++.h>
using namespace std;
int countPermWithkInversions(int n, int k) {
// Initialize a 2D table for dynamic programming
vector<vector<int>> dp(n + 1, vector<int>(k + 1, 0));
// Base case: If k is 0, only one way to
// have 0 inversions: sorted order
for (int l = 0; l <= n; l++) {
dp[l][0] = 1;
}
// Fill the table using the tabulation method
for (int l = 1; l <= n; l++) {
for (int r = 1; r <= k; r++) {
for (int i = 0; i <= min(r, l - 1); i++) {
// Count permutations for the remaining
// elements and inversions
dp[l][r] = (dp[l][r] + dp[l - 1][r - i]);
}
}
}
return dp[n][k];
}
int main() {
int n = 4;
int k = 2;
cout << countPermWithkInversions(n, k);
return 0;
}
Java
// Java code to find number of permutation
// with K inversions using Tabulation
import java.util.Arrays;
class GfG {
static int countPermWithkInversions(int n, int k) {
// Initialize a 2D table for dynamic programming
int[][] dp = new int[n + 1][k + 1];
// Base case: If k is 0, only one way to
// have 0 inversions: sorted order
for (int l = 0; l <= n; l++) {
dp[l][0] = 1;
}
// Fill the table using the tabulation method
for (int l = 1; l <= n; l++) {
for (int r = 1; r <= k; r++) {
for (int i = 0; i <= Math.min(r, l - 1); i++) {
// Count permutations for the remaining
// elements and inversions
dp[l][r] = (dp[l][r] + dp[l - 1][r - i]);
}
}
}
return dp[n][k];
}
public static void main(String[] args) {
int n = 4;
int k = 2;
System.out.println(countPermWithkInversions(n, k));
}
}
Python
# Python code to find number of permutation
# with k inversions using Tabulation
def countPermWithkInversions(n, k):
# Initialize a 2D table for dynamic programming
dp = [[0] * (k + 1) for _ in range(n + 1)]
# Base case: If k is 0, only one way to
# have 0 inversions: sorted order
for l in range(n + 1):
dp[l][0] = 1
# Fill the table using the tabulation method
for l in range(1, n + 1):
for r in range(1, k + 1):
for i in range(min(r, l - 1) + 1):
# Count permutations for the remaining
# elements and inversions
dp[l][r] = (dp[l][r] + dp[l - 1][r - i])
return dp[n][k]
if __name__ == "__main__":
n = 4
k = 2
print(countPermWithkInversions(n, k))
C#
// C# code to find number of permutation
// with k inversions using Tabulation
using System;
class GfG {
static int countPermWithkInversions(int n, int k) {
// Initialize a 2D table for dynamic programming
int[,] dp = new int[n + 1, k + 1];
// Base case: If k is 0, only one way to
// have 0 inversions: sorted order
for (int l = 0; l <= n; l++) {
dp[l, 0] = 1;
}
// Fill the table using the tabulation method
for (int l = 1; l <= n; l++) {
for (int r = 1; r <= k; r++) {
for (int i = 0; i <= Math.Min(r, l - 1); i++) {
// Count permutations for the remaining
// elements and inversions
dp[l, r] = (dp[l, r] + dp[l - 1, r - i]);
}
}
}
return dp[n, k];
}
static void Main() {
int n = 4;
int k = 2;
Console.WriteLine(countPermWithkInversions(n, k));
}
}
JavaScript
// JavaScript code to find number of permutation
// with k inversions using Tabulation
function countPermWithkInversions(n, k) {
// Initialize a 2D table for dynamic programming
const dp = Array.from({ length: n + 1 }, () => Array(k + 1).fill(0));
// Base case: If k is 0, only one way to
// have 0 inversions: sorted order
for (let l = 0; l <= n; l++) {
dp[l][0] = 1;
}
// Fill the table using the tabulation method
for (let l = 1; l <= n; l++) {
for (let r = 1; r <= k; r++) {
for (let i = 0; i <= Math.min(r, l - 1); i++) {
// Count permutations for the remaining
// elements and inversions
dp[l][r] = (dp[l][r] + dp[l - 1][r - i]);
}
}
}
return dp[n][k];
}
let n = 4;
let k = 2;
console.log(countPermWithkInversions(n, k));
Time Complexity: O(n*k*k), where n is the number of elements and k is the number of inversions, due to the nested loops iterating through n, k, and possible inversions.
Auxiliary Space: O(n*k), because we maintain a 2D table of size (n+1)×(k+1) to store the computed values for dynamic programming.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem