Count of Derangements of given Array with Repetition
Last Updated :
23 Jul, 2025
Given an array arr[] of N numbers (N ≤ 20), the task is to find the number of Derangements of the array where array elements can be repeated.
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}.
Examples:
Input: arr[] = {0, 0, 1}
Output: 0
Explanation: All permutations of the array are - {0, 0, 1}, {0, 1, 0} and{1, 0, 0}.
In each permutation, there is atleast one number which is placed in the same position as in the input array.
Hence the answer is 0.
Input: arr[] = {1, 1, 0, 0}
Output: 1
Explanation: The only derangement is {0, 0, 1, 1}
Input: arr[] = {1, 2, 2, 3, 3}
Output: 4
Explanation: The derangements are {2, 3, 3, 1, 2}, {2, 3, 3, 2, 1}, {3, 3, 1, 2, 2} and {3, 1, 3, 2, 2}
Naive Approach: The simplest approach to solve this problem is to generate all possible permutations and for each permutation, check if all the elements are not placed in their original position. If found to be true, then increment the count. Finally, print the count.
Time Complexity: O(N! * N)
Auxiliary Space: O(N)
Efficient Approach: The above approach can also be optimized using dynamic programming and bitmasking as it has overlapping subproblems and optimal substructure. The idea to solve this problem is based on the following observations:
- For each index of Arr[] the idea is to choose a not selected array element of Arr[] and using bitmasking to keep track of the already selected elements of Arr[]. The selected element must also be not equal to the current element to ensure derangement.
- dp(i, mask) stores the result from the 'i'th position till the end, with bitmask 'mask' denoting the already selected elements of array Arr[] till the 'i-1'th position.
- Since the current position can be determined by the count of set bits of mask, reduce dp(i, mask) to dp(mask).
- Then the transition from one state to another state can be defined as:
- For all j in the range [0, N - 1]:
- If the jth bit of mask is not set and if A[i] != A[j], then, dp(i, mask) += dp(i + 1, mask|(1<<j)).
Follow the steps below to solve the problem:
- Define a dp array of size (1 << N) and initialize it with -1 to store all dp states.
- Define a recursive function say countDerangements(i, mask) to count the number of derangements
- Base case, if i is equal to N then a valid derangement has been constructed.
- If dp[mask] is not equal to -1 i.e already visited then return dp[mask].
- Iterate over the range [0, N-1] using variable j and in each iteration, if jth bit in the mask is not set and if A[i] != A[j], then,
- Update dp[mask] as dp[mask] = dp[mask] + countDerangements (i+1, mask | (<< j)).
- Finally, return dp[mask].
- If any number is repeated more than once, then the answer returned has to be divided by the frequency of that number because the same number swapped in different positions will result in the same derangement.
- Hence, check for the frequency of all numbers and divide the answer by the frequency of a number if it is greater than 1.
- This will be the final answer.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Declaring a dp array
long long dp[1 << 20];
// Function to return
// the factorial of a number
long long factorial(int n)
{
long long fact = 1;
for (int i = 2; i <= n; ++i) {
fact *= i;
}
return fact;
}
// Function to count the
// Number of derangements
long long countDerangements(int i, int mask,
int n, int A[])
{
// If all the numbers are placed,
// then return 1.
if (mask == (1 << n) - 1) {
return 1;
}
// If the state has already been computed,
// then return it
if (dp[mask] != -1) {
return dp[mask];
}
dp[mask] = 0;
// Check for all possible numbers
// that can be placed in
// the current position 'i'.
for (int j = 0; j < n; ++j) {
// If the current element arr[j]
// Is not yet selected
//(bit 'j' is unset in mask),
// And if arr[i]!= arr[j]
//(to ensure derangement).
if (!(mask & (1 << j))
and (A[i] != A[j])) {
// Set the bit 'j' in mask and
// Call the function
// For index 'i + 1'.
dp[mask] += countDerangements(
i + 1, mask | (1 << j), n, A);
}
}
// Return dp[mask]
return dp[mask];
}
// Utility Function to count
// The number of derangements
long long UtilCountDerangements(int A[],
int N)
{
// Initializing the dp array with -1.
memset(dp, -1, sizeof dp);
// HashMap to store the frequency
// of each number.
map<int, int> frequencyMap;
for (int i = 0; i < N; ++i) {
++frequencyMap[A[i]];
}
// Function call and storing
// The return value in 'ans'.
long long ans
= countDerangements(0, 0, N, A);
// Iterating through the HashMap
for (auto itr : frequencyMap) {
// Frequency of current number
int times = itr.second;
// If it occurs more than 1 time,
// divide the answer by its frequency.
if (times > 1) {
ans /= factorial(times);
}
}
return ans;
}
// Driver code
int main()
{
// Input array
int arr[] = { 1, 2, 2, 3, 3 };
// Size of array
int N = sizeof(arr) / sizeof(arr[0]);
cout << UtilCountDerangements(arr, N);
return 0;
}
Java
// Java program to implement the approach
import java.util.*;
public class Main
{
// Declaring a dp array
static int[] dp = new int[1 << 20];
// Function to return
// the factorial of a number
static int factorial(int n)
{
int fact = 1;
for (int i = 2; i <= n; ++i) {
fact *= i;
}
return fact;
}
// Function to count the
// Number of derangements
static int countDerangements(int i, int mask,
int n, int[] A)
{
// If all the numbers are placed,
// then return 1.
if (mask == (1 << n) - 1) {
return 1;
}
// If the state has already been computed,
// then return it
if (dp[mask] != -1) {
return dp[mask];
}
dp[mask] = 0;
// Check for all possible numbers
// that can be placed in
// the current position 'i'.
for (int j = 0; j < n; ++j) {
// If the current element arr[j]
// Is not yet selected
//(bit 'j' is unset in mask),
// And if arr[i]!= arr[j]
//(to ensure derangement).
if ((mask & (1 << j)) == 0
&& (A[i] != A[j])) {
// Set the bit 'j' in mask and
// Call the function
// For index 'i + 1'.
dp[mask] += countDerangements(
i + 1, mask | (1 << j), n, A);
}
}
// Return dp[mask]
return dp[mask];
}
// Utility Function to count
// The number of derangements
static int UtilCountDerangements(int[] A,
int N)
{
// Initializing the dp array with -1.
for(int i = 0;i<(1 << 20); i++)
{
dp[i]=-1;
}
// HashMap to store the frequency
// of each number.
HashMap<Integer,
Integer> frequencyMap = new HashMap<Integer,
Integer>();
for (int i = 0; i < N; i++) {
// Counting freq of each element
if(frequencyMap.containsKey(A[i])){
frequencyMap.put(A[i], frequencyMap.get(A[i])+1);
}else{
frequencyMap.put(A[i], 1);
}
}
// Function call and storing
// The return value in 'ans'.
int ans
= countDerangements(0, 0, N, A);
// Iterating through the HashMap
for (Map.Entry<Integer,Integer> itr : frequencyMap.entrySet())
{
// Frequency of current number
int times = itr.getValue();
// If it occurs more than 1 time,
// divide the answer by its frequency.
if (times > 1) {
ans /= factorial(times);
}
}
return ans;
}
// Driver code
public static void main(String[] args)
{
// Input array
int[] arr = { 1, 2, 2, 3, 3 };
// Size of array
int N = arr.length;
System.out.println( UtilCountDerangements(arr, N));
}
}
// This code is contributed by code_hunt.
Python3
# Python program for the above approach
# Declaring a dp array
dp = [-1]*(1 << 20)
# Function to return
# the factorial of a number
def factorial(n):
fact = 1
for i in range(2,n+1):
fact *= i
return fact
# Function to count the
# Number of derangements
def countDerangements(i,mask,n, A):
# If all the numbers are placed,
# then return 1.
if (mask == (1 << n) - 1):
return 1
# If the state has already been computed,
# then return it
if (dp[mask] != -1):
return dp[mask]
dp[mask] = 0
# Check for all possible numbers
# that can be placed in
# the current position 'i'.
for j in range(n):
# If the current element arr[j]
# Is not yet selected
#(bit 'j' is unset in mask),
# And if arr[i]!= arr[j]
#(to ensure derangement).
if (mask & (1 << j) == 0 and (A[i] != A[j])):
# Set the bit 'j' in mask and
# Call the function
# For index 'i + 1'.
dp[mask] += countDerangements(i + 1, mask | (1 << j), n, A)
# Return dp[mask]
return dp[mask]
# Utility Function to count
# The number of derangements
def UtilCountDerangements(A,N):
# HashMap to store the frequency
# of each number.
frequencyMap = {}
for i in range(N):
if A[i] in frequencyMap:
frequencyMap[A[i]] = frequencyMap[A[i]]+1
else:
frequencyMap[A[i]] = 1
# Function call and storing
# The return value in 'ans'.
ans = countDerangements(0, 0, N, A)
# Iterating through the HashMap
for key,value in frequencyMap.items():
# Frequency of current number
times = value
# If it occurs more than 1 time,
# divide the answer by its frequency.
if (times > 1):
ans = ans // factorial(times)
return ans
# Driver code
# Input array
arr = [ 1, 2, 2, 3, 3 ]
# Size of array
N = len(arr)
print(UtilCountDerangements(arr, N))
# This code is contributed by shinjanpatra.
C#
// C# program of the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Declaring a dp array
static int[] dp = new int[1 << 20];
// Function to return
// the factorial of a number
static int factorial(int n)
{
int fact = 1;
for (int i = 2; i <= n; ++i) {
fact *= i;
}
return fact;
}
// Function to count the
// Number of derangements
static int countDerangements(int i, int mask,
int n, int[] A)
{
// If all the numbers are placed,
// then return 1.
if (mask == (1 << n) - 1) {
return 1;
}
// If the state has already been computed,
// then return it
if (dp[mask] != -1) {
return dp[mask];
}
dp[mask] = 0;
// Check for all possible numbers
// that can be placed in
// the current position 'i'.
for (int j = 0; j < n; ++j) {
// If the current element arr[j]
// Is not yet selected
//(bit 'j' is unset in mask),
// And if arr[i]!= arr[j]
//(to ensure derangement).
if ((mask & (1 << j)) == 0
&& (A[i] != A[j])) {
// Set the bit 'j' in mask and
// Call the function
// For index 'i + 1'.
dp[mask] += countDerangements(
i + 1, mask | (1 << j), n, A);
}
}
// Return dp[mask]
return dp[mask];
}
// Utility Function to count
// The number of derangements
static int UtilCountDerangements(int[] A,
int N)
{
// Initializing the dp array with -1.
for(int i = 0;i<(1 << 20); i++)
{
dp[i]=-1;
}
// HashMap to store the frequency
// of each number.
Dictionary<int,int> frequencyMap = new Dictionary<int,int>();
for (int i = 0; i < N; i++) {
if(frequencyMap.ContainsKey(A[i])){
frequencyMap[A[i]] = frequencyMap[A[i]] + 1;
}else{
frequencyMap.Add(A[i], 1);
}
}
// Function call and storing
// The return value in 'ans'.
int ans
= countDerangements(0, 0, N, A);
// Iterating through the HashMap
foreach (KeyValuePair<int,int> itr in frequencyMap)
{
// Frequency of current number
int times = itr.Value;
// If it occurs more than 1 time,
// divide the answer by its frequency.
if (times > 1) {
ans /= factorial(times);
}
}
return ans;
}
// Driver Code
public static void Main()
{
// Input array
int[] arr = { 1, 2, 2, 3, 3 };
// Size of array
int N = arr.Length;
Console.Write( UtilCountDerangements(arr, N));
}
}
// This code is contributed by sanjoy_62.
JavaScript
<script>
// JavaScript code for the above approach
// Declaring a dp array
let dp = new Array(1 << 20).fill(-1);
// Function to return
// the factorial of a number
function factorial(n) {
let fact = 1;
for (let i = 2; i <= n; ++i) {
fact *= i;
}
return fact;
}
// Function to count the
// Number of derangements
function countDerangements(i, mask,
n, A)
{
// If all the numbers are placed,
// then return 1.
if (mask == (1 << n) - 1) {
return 1;
}
// If the state has already been computed,
// then return it
if (dp[mask] != -1) {
return dp[mask];
}
dp[mask] = 0;
// Check for all possible numbers
// that can be placed in
// the current position 'i'.
for (let j = 0; j < n; ++j) {
// If the current element arr[j]
// Is not yet selected
//(bit 'j' is unset in mask),
// And if arr[i]!= arr[j]
//(to ensure derangement).
if (!(mask & (1 << j))
&& (A[i] != A[j])) {
// Set the bit 'j' in mask and
// Call the function
// For index 'i + 1'.
dp[mask] += countDerangements(
i + 1, mask | (1 << j), n, A);
}
}
// Return dp[mask]
return dp[mask];
}
// Utility Function to count
// The number of derangements
function UtilCountDerangements(A, N) {
// Initializing the dp array with -1
// HashMap to store the frequency
// of each number.
let frequencyMap = new Map();
for (let i = 0; i < N; ++i) {
if (frequencyMap.has(A[i])) {
frequencyMap.set(A[i], frequencyMap.get(A[i]) + 1)
}
else {
frequencyMap.set(A[i], 1)
}
}
// Function call and storing
// The return value in 'ans'.
let ans
= countDerangements(0, 0, N, A);
// Iterating through the HashMap
for (let [key, val] of frequencyMap) {
// Frequency of current number
let times = val;
// If it occurs more than 1 time,
// divide the answer by its frequency.
if (times > 1) {
ans /= factorial(times);
}
}
return ans;
}
// Driver code
// Input array
let arr = [1, 2, 2, 3, 3];
// Size of array
let N = arr.length;
document.write(UtilCountDerangements(arr, N));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N * 2N)
Auxiliary Space: O(2N)
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