Count Derangements (Permutation such that no element appears in its original position)
Last Updated :
23 Jul, 2025
A Derangement is a permutation of n elements, such that no element appears in its original position. For example, a derangement of [0, 1, 2, 3] is [2, 3, 1, 0].
Given a number n, find the total number of Derangements of a set of n elements.
Examples :
Input: n = 2
Output: 1
Explanation: For two balls [1, 2], there is only one possible derangement [2, 1].
Input: n = 3
Output: 2
Explanation: For three balls [1, 2, 3], there are two possible derangements [3, 1, 2] and [2, 3, 1].
Using Recursion - O(2 ^ n) Time and O(n) Space
The idea is to use recursion as, there are n - 1 way for element 0 (this explains multiplication with n - 1).
Let 0 be placed at index i. There are now two possibilities, depending on whether or not element i is placed at 0 in return.
- i is placed at 0: This case is equivalent to solving the problem for n-2 elements as two elements have just swapped their positions.
- i is not placed at 0: This case is equivalent to solving the problem for n-1 elements as now there are n-1 elements, n-1 positions and every element has n-2 choices
The formula used in the solution is:
F(n) = (n - 1) * (F(n - 1) + F(n - 2)
C++
// A Naive Recursive C++ program
// to count derangements
#include <bits/stdc++.h>
using namespace std;
int disarrange(int n) {
// Base cases
if (n == 1) return 0;
if (n == 2) return 1;
// countDer(n) = (n-1)[countDer(n-1) + der(n-2)]
return (n - 1) * (disarrange(n - 1) + disarrange(n - 2));
}
int main() {
int n = 4;
cout << disarrange(n);
return 0;
}
Java
// A Naive Recursive java
// program to count derangements
import java.io.*;
class GfG {
// Function to count
// derangements
static int disarrange(int n) {
// Base cases
if (n == 1) return 0;
if (n == 2) return 1;
// countDer(n) = (n-1)[countDer(n-1) + der(n-2)]
return (n - 1) * (disarrange(n - 1) +
disarrange(n - 2));
}
public static void main (String[] args) {
int n = 4;
System.out.println(disarrange(n));
}
}
Python
# A Naive Recursive Python3
# program to count derangements
def disarrange(n):
# Base cases
if (n == 1): return 0
if (n == 2): return 1
# countDer(n) = (n-1)[countDer(n-1) + der(n-2)]
return (n - 1) * (disarrange(n - 1) +
disarrange(n - 2))
n = 4
print(disarrange(n))
C#
// A Naive Recursive C#
// program to count derangements
using System;
class GfG {
// Function to count
// derangements
static int disarrange(int n) {
// Base cases
if (n == 1) return 0;
if (n == 2) return 1;
// countDer(n) = (n-1)[countDer(n-1) + der(n-2)]
return (n - 1) * (disarrange(n - 1) +
disarrange(n - 2));
}
static void Main () {
int n = 4;
Console.Write(disarrange(n));
}
}
JavaScript
// A Naive Recursive Javascript
// program to count derangements
// Function to calculate derangements
function disarrange(n) {
// Base cases
if (n === 1) return 0;
if (n === 2) return 1;
// Recurrence relation: disarrange(n) = (n-1) * (disarrange(n-1) + disarrange(n-2))
return (n - 1) * (disarrange(n - 1) + disarrange(n - 2));
}
const n = 4;
console.log(disarrange(n));
Using Top-Down DP (Memoization) - O(n) Time and O(n) Space
Above recursive solution has properties that make it suitable for optimization using Dynamic Programming:
1. Optimal Substructure: The recursive relation demonstrates that the solution for F(n) can be constructed using the solutions to smaller subproblems, specifically F(n - 1) and F(n - 2).
2. Overlapping Subproblems: The function disarrange(n) recursively calls itself with n - 1 and n - 2. However, these subproblems are not independent. For example, when calculating disarrange(4), it will internally compute disarrange(3) and disarrange(2). Then, disarrange(3) will again compute disarrange(2), leading to redundant calculations.
C++
// A Dynamic programming based C++
// program to count derangements
#include <bits/stdc++.h>
using namespace std;
int calculate(int n, vector<int> &memo) {
// Base cases
if (n == 1) return 0;
if (n == 2) return 1;
// If the value of n is previously
// calculated then return it here
if(memo[n] != -1) return memo[n];
// countDer(n) = (n-1)[countDer(n-1) + der(n-2)]
return memo[n] = (n - 1) * (calculate(n - 1, memo)
+ calculate(n - 2, memo));
}
int disarrange(int n) {
vector<int> memo(n + 1, -1);
return calculate(n, memo);
}
int main() {
int n = 4;
cout << disarrange(n);
return 0;
}
Java
// A Dynamic programming based Java program
// to count derangements
import java.util.*;
class GfG {
static int calculate(int n, int[] memo) {
// Base cases
if (n == 1) return 0;
if (n == 2) return 1;
// If the value of n is previously calculated
// then return it here
if (memo[n] != -1) return memo[n];
// countDer(n) = (n-1)[countDer(n-1) + der(n-2)]
return memo[n] = (n - 1) * (calculate(n - 1, memo)
+ calculate(n - 2, memo));
}
static int disarrange(int n) {
int[] memo = new int[n + 1];
Arrays.fill(memo, -1);
return calculate(n, memo);
}
public static void main(String[] args) {
int n = 4;
System.out.println(disarrange(n));
}
}
Python
# A Dynamic programming based Python
# program to count derangements
def calculate(n, memo):
# Base cases
if n == 1:
return 0
if n == 2:
return 1
# If the value of n is previously
# calculated then return it here
if memo[n] != -1:
return memo[n]
# countDer(n) = (n-1)[countDer(n-1) + der(n-2)]
memo[n] = (n - 1) * (calculate(n - 1, memo) + calculate(n - 2, memo))
return memo[n]
def disarrange(n):
memo = [-1] * (n + 1)
return calculate(n, memo)
n = 4
print(disarrange(n))
C#
// A Dynamic programming based C#
// program to count derangements
using System;
class GfG {
static int Calculate(int n, int[] memo) {
// Base cases
if (n == 1) return 0;
if (n == 2) return 1;
// If the value of n is previously
// calculated then return it here
if (memo[n] != -1) return memo[n];
// countDer(n) = (n-1)[countDer(n-1) + der(n-2)]
memo[n] = (n - 1) * (Calculate(n - 1, memo) + Calculate(n - 2, memo));
return memo[n];
}
static int Disarrange(int n) {
int[] memo = new int[n + 1];
Array.Fill(memo, -1);
return Calculate(n, memo);
}
static void Main() {
int n = 4;
Console.WriteLine(Disarrange(n));
}
}
JavaScript
// A Dynamic programming based
// JavaScript program to count derangements
function calculate(n, memo) {
// Base cases
if (n === 1) return 0;
if (n === 2) return 1;
// If the value of n is previously
// calculated then return it here
if (memo[n] !== -1) return memo[n];
// countDer(n) = (n-1)[countDer(n-1) + der(n-2)]
memo[n] = (n - 1) * (calculate(n - 1, memo) + calculate(n - 2, memo));
return memo[n];
}
function disarrange(n) {
let memo = Array(n + 1).fill(-1);
return calculate(n, memo);
}
const n = 4;
console.log(disarrange(n));
Using Bottom-Up DP (Tabulation) - O(n) Time and O(n) Space
The approach is similar to the previous one. just instead of breaking down the problem recursively, we iteratively build up the solution by calculating in bottom-up manner. Maintain a dp[] table such that dp[i] stores the Count Derangements for i.
Base Case:
- For i = 1 dp[i] = 0
- for i = 2 dp[i] = 1
Recursive Case:
- For i > 2 dp[i] = (i - 1) * (dp[i-1] + dp[i - 1])
C++
// Optimized DP-based solution to
// count derangements
#include <bits/stdc++.h>
using namespace std;
int disarrange(int n) {
// Create a DP array to store
// results
vector<int> dp(n + 1);
// Base cases
dp[1] = 0;
dp[2] = 1;
// Fill the DP array using the recursive relation
for (int i = 3; i <= n; i++) {
dp[i] = (i - 1) * (dp[i - 1] + dp[i - 2]);
}
return dp[n];
}
int main() {
int n = 4;
cout << disarrange(n);
return 0;
}
Java
// Optimized DP-based solution to
// count derangements
import java.util.*;
class GfG {
static int disarrange(int n) {
// Create a DP array to store results
int[] dp = new int[n + 1];
// Base cases
dp[1] = 0;
dp[2] = 1;
// Fill the DP array using the recursive relation
for (int i = 3; i <= n; i++) {
dp[i] = (i - 1) * (dp[i - 1] + dp[i - 2]);
}
return dp[n];
}
public static void main(String[] args) {
int n = 4;
System.out.println(disarrange(n));
}
}
Python
# Optimized DP-based solution
# to count derangements
def disarrange(n):
# Create a DP array to store results
dp = [0] * (n + 1)
# Base cases
dp[1] = 0
dp[2] = 1
# Fill the DP array using the
# recursive relation
for i in range(3, n + 1):
dp[i] = (i - 1) * (dp[i - 1] + dp[i - 2])
return dp[n]
n = 4
print(disarrange(n))
C#
// Optimized DP-based solution to count derangements
using System;
class GfG {
static int Disarrange(int n) {
// Create a DP array to store results
int[] dp = new int[n + 1];
// Base cases
dp[1] = 0;
dp[2] = 1;
// Fill the DP array using the recursive relation
for (int i = 3; i <= n; i++) {
dp[i] = (i - 1) * (dp[i - 1] + dp[i - 2]);
}
return dp[n];
}
static void Main() {
int n = 4;
Console.WriteLine(Disarrange(n));
}
}
JavaScript
// Optimized DP-based solution to count derangements
function disarrange(n) {
// Create a DP array to store results
const dp = new Array(n + 1).fill(0);
// Base cases
dp[1] = 0;
dp[2] = 1;
// Fill the DP array using the recursive relation
for (let i = 3; i <= n; i++) {
dp[i] = (i - 1) * (dp[i - 1] + dp[i - 2]);
}
return dp[n];
}
const n = 4;
console.log(disarrange(n));
Using Space Optimized DP - O(n) Time and O(1) Space
To optimize the space complexity to O(1) we can avoid using an array and instead use two variables to store the previous two results, as we only need the last two values to compute the next value. This will reduce the space requirement significantly.
C++
// Optimized DP-based solution to count
// derangements with O(1) space
#include <bits/stdc++.h>
using namespace std;
int disarrange(int n) {
// Base cases
if (n == 1) return 0;
if (n == 2) return 1;
// Variables to store previous two results
// Equivalent to dp[1]
int prev2 = 0;
// Equivalent to dp[2]
int prev1 = 1;
// Calculate derangements using the optimized
// space approach
for (int i = 3; i <= n; i++) {
int curr = (i - 1) * (prev1 + prev2);
prev2 = prev1;
prev1 = curr;
}
return prev1;
}
int main() {
int n = 4;
cout << disarrange(n);
return 0;
}
Java
// Optimized DP-based solution to count
// derangements with O(1) space
class GfG {
static int disarrange(int n) {
// Base cases
if (n == 1) return 0;
if (n == 2) return 1;
// Variables to store previous two results
// Equivalent to dp[1]
int prev2 = 0;
// Equivalent to dp[2]
int prev1 = 1;
// Calculate derangements using the
// optimized space approach
for (int i = 3; i <= n; i++) {
int curr = (i - 1) * (prev1 + prev2);
prev2 = prev1;
prev1 = curr;
}
return prev1;
}
public static void main(String[] args) {
int n = 4;
System.out.println(disarrange(n));
}
}
Python
# Optimized DP-based solution to count
# derangements with O(1) space
def disarrange(n):
# Base cases
if n == 1:
return 0
if n == 2:
return 1
# Variables to store previous two results
# Equivalent to dp[1]
prev2 = 0
# Equivalent to dp[2]
prev1 = 1
# Calculate derangements using the
# optimized space approach
for i in range(3, n + 1):
curr = (i - 1) * (prev1 + prev2)
prev2 = prev1
prev1 = curr
return prev1
n = 4
print(disarrange(n))
C#
// Optimized DP-based solution to count
// derangements with O(1) space
using System;
class GfG {
static int Disarrange(int n) {
// Base cases
if (n == 1) return 0;
if (n == 2) return 1;
// Variables to store previous two results
// Equivalent to dp[1]
int prev2 = 0;
// Equivalent to dp[2]
int prev1 = 1;
// Calculate derangements using the optimized
// space approach
for (int i = 3; i <= n; i++) {
int curr = (i - 1) * (prev1 + prev2);
prev2 = prev1;
prev1 = curr;
}
return prev1;
}
static void Main() {
int n = 4;
Console.WriteLine(Disarrange(n));
}
}
JavaScript
// Optimized DP-based solution to count
// derangements with O(1) space
function disarrange(n) {
// Base cases
if (n === 1) return 0;
if (n === 2) return 1;
// Variables to store previous two results
// Equivalent to dp[1]
let prev2 = 0;
// Equivalent to dp[2]
let prev1 = 1;
// Calculate derangements using the
// optimized space approach
for (let i = 3; i <= n; i++) {
let curr = (i - 1) * (prev1 + prev2);
prev2 = prev1;
prev1 = curr;
}
return prev1;
}
const n = 4;
console.log(disarrange(n));
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