# Count permutation such that sequence is non decreasing

Given an array arr[] of integers, the task is to find the count of permutation of the array such that the permutation is in increasing order i.e. arr ≤ arr ≤ arr ≤ … ≤ arr[n – 1].

Examples:

Input: arr[] = {1, 2, 1}
Output: 2
1, 1, 2 and 1, 1, 2 are the only valid permutations.

Input: arr[] = {5, 4, 4, 5}
Output: 4

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The sequence should be non-descending i.e. arr ≤ arr ≤ arr ≤ … ≤ arr[n – 1].
First, sort the array and then focus on the block where all elements are equal as these elements can be rearranged in P! ways where P is the size of that block.
Permuting that block will not violate the given condition. Now, find all the blocks where all elements are equal and multiply the answer of that individual block to the final answer to get the total count of possible permutations.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `#define N 20 ` ` `  `// To store the factorials ` `int` `fact[N]; ` ` `  `// Function to update fact[] array ` `// such that fact[i] = i! ` `void` `pre() ` `{ ` ` `  `    ``// 0! = 1 ` `    ``fact = 1; ` `    ``for` `(``int` `i = 1; i < N; i++) { ` ` `  `        ``// i! = i * (i - 1)! ` `        ``fact[i] = i * fact[i - 1]; ` `    ``} ` `} ` ` `  `// Function to return the count ` `// of possible permutations ` `int` `CountPermutation(``int` `a[], ``int` `n) ` `{ ` ` `  `    ``// To store the result ` `    ``int` `ways = 1; ` ` `  `    ``// Sort the array ` `    ``sort(a, a + n); ` ` `  `    ``// Initial size of the block ` `    ``int` `size = 1; ` `    ``for` `(``int` `i = 1; i < n; i++) { ` ` `  `        ``// Increase the size of block ` `        ``if` `(a[i] == a[i - 1]) { ` `            ``size++; ` `        ``} ` `        ``else` `{ ` ` `  `            ``// Update the result for ` `            ``// the previous block ` `            ``ways *= fact[size]; ` ` `  `            ``// Reset the size to 1 ` `            ``size = 1; ` `        ``} ` `    ``} ` ` `  `    ``// Update the result for ` `    ``// the last block ` `    ``ways *= fact[size]; ` ` `  `    ``return` `ways; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` ` `  `    ``int` `a[] = { 1, 2, 4, 4, 2, 4 }; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a); ` ` `  `    ``// Pre-calculating factorials ` `    ``pre(); ` ` `  `    ``cout << CountPermutation(a, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `//Java implementation of the approach ` `import` `java.util.Arrays;  ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `static` `int` `N = ``20``; ` ` `  `// To store the factorials ` `static` `int` `[]fact=``new` `int``[N]; ` ` `  `// Function to update fact[] array ` `// such that fact[i] = i! ` `static` `void` `pre() ` `{ ` ` `  `    ``// 0! = 1 ` `    ``fact[``0``] = ``1``; ` `    ``for` `(``int` `i = ``1``; i < N; i++) ` `    ``{ ` ` `  `        ``// i! = i * (i - 1)! ` `        ``fact[i] = i * fact[i - ``1``]; ` `    ``} ` `} ` ` `  `// Function to return the count ` `// of possible permutations ` `static` `int` `CountPermutation(``int` `a[], ``int` `n) ` `{ ` ` `  `    ``// To store the result ` `    ``int` `ways = ``1``; ` ` `  `    ``// Sort the array ` `    ``Arrays.sort(a); ` ` `  `    ``// Initial size of the block ` `    ``int` `size = ``1``; ` `    ``for` `(``int` `i = ``1``; i < n; i++)  ` `    ``{ ` ` `  `        ``// Increase the size of block ` `        ``if` `(a[i] == a[i - ``1``])  ` `        ``{ ` `            ``size++; ` `        ``} ` `        ``else` `        ``{ ` ` `  `            ``// Update the result for ` `            ``// the previous block ` `            ``ways *= fact[size]; ` ` `  `            ``// Reset the size to 1 ` `            ``size = ``1``; ` `        ``} ` `    ``} ` ` `  `    ``// Update the result for ` `    ``// the last block ` `    ``ways *= fact[size]; ` ` `  `    ``return` `ways; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``int` `a[] = { ``1``, ``2``, ``4``, ``4``, ``2``, ``4` `}; ` `    ``int` `n = a.length; ` `     `  `    ``// Pre-calculating factorials ` `    ``pre(); ` `     `  `    ``System.out.println (CountPermutation(a, n)); ` `} ` `} ` ` `  `// This code is contributed by Sachin `

## Python3

 `# Python3 implementation of the approach  ` `N ``=` `20` ` `  `# To store the factorials  ` `fact ``=` `[``0``] ``*` `N;  ` ` `  `# Function to update fact[] array  ` `# such that fact[i] = i!  ` `def` `pre() : ` ` `  `    ``# 0! = 1  ` `    ``fact[``0``] ``=` `1``;  ` `    ``for` `i ``in` `range``(``1``, N): ` ` `  `        ``# i! = i * (i - 1)!  ` `        ``fact[i] ``=` `i ``*` `fact[i ``-` `1``];  ` ` `  `# Function to return the count  ` `# of possible permutations  ` `def` `CountPermutation(a, n):  ` ` `  `    ``# To store the result  ` `    ``ways ``=` `1``;  ` ` `  `    ``# Sort the array  ` `    ``a.sort(); ` ` `  `    ``# Initial size of the block  ` `    ``size ``=` `1``;  ` `    ``for` `i ``in` `range``(``1``, n): ` ` `  `        ``# Increase the size of block  ` `        ``if` `(a[i] ``=``=` `a[i ``-` `1``]): ` `            ``size ``+``=` `1``;  ` `         `  `        ``else` `: ` ` `  `            ``# Update the result for  ` `            ``# the previous block  ` `            ``ways ``*``=` `fact[size];  ` ` `  `            ``# Reset the size to 1  ` `            ``size ``=` `1``;  ` ` `  `    ``# Update the result for  ` `    ``# the last block  ` `    ``ways ``*``=` `fact[size];  ` ` `  `    ``return` `ways;  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``a ``=` `[ ``1``, ``2``, ``4``, ``4``, ``2``, ``4` `];  ` `    ``n ``=` `len``(a);  ` ` `  `    ``# Pre-calculating factorials  ` `    ``pre();  ` ` `  `    ``print``(CountPermutation(a, n));  ` `     `  `# This code is contributed by AnkitRai01 `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `static` `int` `N = 20; ` ` `  `// To store the factorials ` `static` `int` `[]fact = ``new` `int``[N]; ` ` `  `// Function to update fact[] array ` `// such that fact[i] = i! ` `static` `void` `pre() ` `{ ` ` `  `    ``// 0! = 1 ` `    ``fact = 1; ` `    ``for` `(``int` `i = 1; i < N; i++) ` `    ``{ ` ` `  `        ``// i! = i * (i - 1)! ` `        ``fact[i] = i * fact[i - 1]; ` `    ``} ` `} ` ` `  `// Function to return the count ` `// of possible permutations ` `static` `int` `CountPermutation(``int` `[]a, ``int` `n) ` `{ ` ` `  `    ``// To store the result ` `    ``int` `ways = 1; ` ` `  `    ``// Sort the array ` `    ``Array.Sort(a); ` ` `  `    ``// Initial size of the block ` `    ``int` `size = 1; ` `    ``for` `(``int` `i = 1; i < n; i++)  ` `    ``{ ` ` `  `        ``// Increase the size of block ` `        ``if` `(a[i] == a[i - 1])  ` `        ``{ ` `            ``size++; ` `        ``} ` `        ``else` `        ``{ ` ` `  `            ``// Update the result for ` `            ``// the previous block ` `            ``ways *= fact[size]; ` ` `  `            ``// Reset the size to 1 ` `            ``size = 1; ` `        ``} ` `    ``} ` ` `  `    ``// Update the result for ` `    ``// the last block ` `    ``ways *= fact[size]; ` ` `  `    ``return` `ways; ` `} ` ` `  `// Driver Code ` `static` `public` `void` `Main () ` `{ ` `    ``int` `[]a = { 1, 2, 4, 4, 2, 4 }; ` `    ``int` `n = a.Length; ` `     `  `    ``// Pre-calculating factorials ` `    ``pre(); ` `     `  `    ``Console.Write(CountPermutation(a, n)); ` `} ` `} ` ` `  `// This code is contributed by Sachin. `

Output:

```12
```

Time Complexity: O(N * logN)

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : AnkitRai01, Sach_Code