- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Count Derangements (Permutation such that no element appears in its original position) in C++

Derangement is permutation of N numbers such that no number appears at original position. For example one possible derangement of { 1,2,3 } is { 2,1,3 }. No element in this is at its original position. The goal here is to count the possible derangements for N numbers.

We will do this using a recursive solution. For following no. of elements −

- N=0, no derangement, return 1
- N=1, only one number, return 0
- N=2, only one swap of position possible, { 1,2 } → { 2,1 }, return 1
- N=3, 2 possible permutations eg, { 1,2,3 } → { 2,3,1 }, { 3,1,2 } count 2
- N=4, 9 possible permutations
- .......................
- N, (N-1)*( permutation(N-1) + permutation(N-2) )

For all elements in an array,

Element at index 0 has n-1 positions that it can take,

If any element at index i is placed at index 0 then, arr[i] and arr[0] are swapped. Now n-2 elements are there for calculation.

If element at index i is not placed at index 0 then for n-1 elements there are n-2 choices.

**Diagram**

## Input

Arr[] = { 1, 2 }

## Output

No. of derangements : 1

**Explanation** − Positions of 1 and 2 are index 0 and 1. Only position they can get is swapping original positions. { 2,1 }

## Input

Arr[] = { 1, 2, 3 }

## Output

No. of derangements : 2

**Explanation** − Positions of 1,2 and 3 are index 0,1,2.

1 can be placed at index 1 and 2, 2 can be placed at index 0 and 3 and 3 can be placed at index 0 and 1.

{ 2,3,1 } and { 3,1,2 } are 2 derangements.

## Approach used in the below program is as follows

Integer Num stores the count of numbers present.

Recursive function derangements(int N) takes the count of numbers as input and returns the no. of derangements.

The return statements for N=0,1,2 are handling the base cases for which permutations are already calculated as 1,0 and 1.

When N>2 then recursive calls to derangements() is done using the formula,

(N-1)* ( derangements ( N-1) + derangements ( N-2) ).

When backtracking starts, the count is computed and returned.

## Example

#include <bits/stdc++.h> using namespace std; int derangements(int N){ if (N == 0) return 1; if (N == 1) return 0; if (N == 2) return 1; return (N - 1) * (derangements(N - 1) + derangements(N - 2)); } int main(){ int Numbers=5; cout<<"Number of Derangements :"<<derangements(Numbers); }

## Output

Number of Derangements :44

- Related Articles
- Maximum difference between two elements such that larger element appears after the smaller number in C
- Find the element that appears once in an array where every other element appears twice in C++
- Find the element that appears once in sorted array - JavaScript
- Count Vowels Permutation in C++
- Count pairs in an array such that at least one element is prime in C++
- Finding ‘k’ such that its modulus with each array element is same in C++
- 8085 program to find the element that appears once
- First element that appears even number of times in an array in C++
- Count natural numbers whose all permutation are greater than that number in C++
- Maximum sum such that no two elements are adjacent in C++
- Rearrange an array such that every odd indexed element is greater than its previous in C++
- Find the only element that appears b times using C++
- Maximum subsequence sum such that no three are consecutive in C++ Program
- Maximum subsequence sum such that no three are consecutive
- Maximum sum in circular array such that no two elements are adjacent in C++