Given any sequence , find the **largest derangement** of .

A derangement is any permutation of , such that no two elements at the same position in and are equal.

The Largest Derangement is such that .

**Examples: **

Input : seq[] = {5, 4, 3, 2, 1} Output : 4 5 2 1 3 Input : seq[] = {56, 21, 42, 67, 23, 74} Output : 74, 67, 56, 42, 21, 23

Since we are interested in generating largest derangement, we start putting larger elements in more significant positions.

Start from left, at any position place the next largest element among the values of the sequence which have not yet been placed in positions before .

To scan all positions takes N iteration. In each iteration we are required to find a maximum numbers, so a trivial implementation would be complexity,

However if we use a data structure like max-heap to find the maximum element, then the complexity reduces to

Below is C++ implementation.

`// CPP program to find the largest derangement ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `printLargest(` `int` `seq[], ` `int` `N) ` `{ ` ` ` `int` `res[N]; ` `// Stores result ` ` ` ` ` `// Insert all elements into a priority queue ` ` ` `std::priority_queue<` `int` `> pq; ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `pq.push(seq[i]); ` ` ` ` ` `// Fill Up res[] from left to right ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `int` `d = pq.top(); ` ` ` `pq.pop(); ` ` ` `if` `(d != seq[i] || i == N - 1) { ` ` ` `res[i] = d; ` ` ` `} ` `else` `{ ` ` ` ` ` `// New Element poped equals the element ` ` ` `// in original sequence. Get the next ` ` ` `// largest element ` ` ` `res[i] = pq.top(); ` ` ` `pq.pop(); ` ` ` `pq.push(d); ` ` ` `} ` ` ` `} ` ` ` ` ` `// If given sequence is in descending order then ` ` ` `// we need to swap last two elements again ` ` ` `if` `(res[N - 1] == seq[N - 1]) { ` ` ` `res[N - 1] = res[N - 2]; ` ` ` `res[N - 2] = seq[N - 1]; ` ` ` `} ` ` ` ` ` `printf` `(` `"\nLargest Derangement \n"` `); ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `printf` `(` `"%d "` `, res[i]); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `seq[] = { 92, 3, 52, 13, 2, 31, 1 }; ` ` ` `int` `n = ` `sizeof` `(seq)/` `sizeof` `(seq[0]); ` ` ` `printLargest(seq, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

Sequence: 92 3 52 13 2 31 1 Largest Derangement 52 92 31 3 13 1 2

**Note:**

The method can be easily modified to obtain the smallest derangement as well.

Instead of a **Max Heap**, we should use a **Min Heap** to consecutively get minimum elements

This article is contributed by **Sayan Mahapatra**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Smallest Derangement of Sequence
- k-th missing element in increasing sequence which is not present in a given sequence
- Minimum operations required to transform a sequence of numbers to a sequence where a[i]=a[i+2]
- Find original sequence from Array containing the sequence merged many times in order
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..
- Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array
- Jolly Jumper Sequence
- Find bitonic point in given bitonic sequence
- Maximum sum Bi-tonic Sub-sequence
- Delete consecutive same words in a sequence
- Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array | Set-2
- Longest sequence of positive integers in an array
- Find minimum length sub-array which has given sub-sequence in it
- Maximum product quadruple (sub-sequence of size 4) in array
- Find the lexicographical next balanced bracket sequence
- Count permutation such that sequence is non decreasing
- Queries on insertion of an element in a Bitonic Sequence
- Find an element in an array such that elements form a strictly decreasing and increasing sequence
- Minimum array elements to be changed to make it a Lucas Sequence
- Minimum number of elements to be replaced to make the given array a Fibonacci Sequence