# Count number of permutation of an Array having no SubArray of size two or more from original Array

Given an array of **distinct** integer **A**, the task is to count the number of possible permutations of the given array A[] such that the permutations do not contain any subarray of size 2 or more from the original array.

**Examples:**

Input:A = [ 1, 3, 9 ]

Output:3

All the permutation of [ 1, 3, 9 ] are : [ 1, 3, 9 ], [ 1, 9, 3 ], [ 3, 9, 1 ], [ 3, 1, 9 ], [ 9, 1, 3 ], [ 9, 3, 1 ]Here [ 1, 3, 9 ], [ 9, 1, 3 ] are removed as they contain sub-array [ 1, 3 ] from original list

and [ 3, 9, 1 ] removed as it contains sub-array [3, 9] from original list so,

Following are the 3 arrays that satisfy the condition : [1, 9, 3], [3, 1, 9], [9, 3, 1]

Input :A = [1, 3, 9, 12]

Output :11

**Naive Approach: **Iterate through list of all permutations and remove those arrays which contains any sub-array **[ i, i+1 ]** from **A**.

Below is the implementation of the above approach:

`# Python implementation of the approach ` ` ` `# Importing the itertools ` `from` `itertools ` `import` `permutations ` ` ` `# Function that return count of all the permutation ` `# having no sub-array of [ i, i + 1 ] ` `def` `count(arr): ` ` ` `z ` `=` `[] ` ` ` `perm ` `=` `permutations(arr) ` ` ` `for` `i ` `in` `list` `(perm): ` ` ` `z.append(` `list` `(i)) ` ` ` ` ` `q ` `=` `[] ` ` ` `for` `i ` `in` `range` `(` `len` `(arr)` `-` `1` `): ` ` ` `x, y ` `=` `arr[i], arr[i ` `+` `1` `] ` ` ` `for` `j ` `in` `range` `(` `len` `(z)): ` ` ` ` ` `# Finding the indexes where x is present ` ` ` `if` `z[j].index(x)!` `=` `len` `(z[j])` `-` `1` `: ` ` ` ` ` `# If y is present at position of x + 1 ` ` ` `# append into a temp list q ` ` ` `if` `z[j][z[j].index(x)` `+` `1` `]` `=` `=` `y: ` ` ` `q.append(z[j]) ` ` ` ` ` `# Removing all the lists that are present ` ` ` `# in z ( list of all premutations ) ` ` ` `for` `i ` `in` `range` `(` `len` `(q)): ` ` ` `if` `q[i] ` `in` `z: ` ` ` `z.remove(q[i]) ` ` ` `return` `len` `(z) ` ` ` `# Driver Code ` `A ` `=` `[` `1` `, ` `3` `, ` `9` `] ` `print` `(count(A)) ` |

*chevron_right*

*filter_none*

**Output:**

3

**Efficient Solution :** After making the solution for smaller size of array, we can observe a pattern:

The following pattern generates a recurrence:

Suppose the length of array **A** is n, then:

count(0) = 1 count(1) = 1 count(n) = n * count(n-1) + (n-1) * count(n-2)

Below is the implementation of the approach:

## C++

`// C++ implementation of the approach ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Recursive function that returns ` `// the count of permutation-based ` `// on the length of the array. ` `int` `count(` `int` `n) ` `{ ` ` ` `if` `(n == 0) ` ` ` `return` `1; ` ` ` `if` `(n == 1) ` ` ` `return` `1; ` ` ` `else` ` ` `return` `(n * count(n - 1)) + ` ` ` `((n - 1) * count(n - 2)); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `A[] = {1, 2, 3, 9}; ` ` ` ` ` `// length of array ` ` ` `int` `n = 4; ` ` ` ` ` `// Output required answer ` ` ` `cout << count(n - 1); ` ` ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by Sanjit Prasad ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Recursive function that returns ` `// the count of permutation-based ` `// on the length of the array. ` `static` `int` `count(` `int` `n) ` `{ ` ` ` `if` `(n == ` `0` `) ` ` ` `return` `1` `; ` ` ` `if` `(n == ` `1` `) ` ` ` `return` `1` `; ` ` ` `else` ` ` `return` `(n * count(n - ` `1` `)) + ` ` ` `((n - ` `1` `) * count(n - ` `2` `)); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `A[] = {` `1` `, ` `2` `, ` `3` `, ` `9` `}; ` ` ` ` ` `// length of array ` ` ` `int` `n = ` `4` `; ` ` ` ` ` `// Output required answer ` ` ` `System.out.println(count(n - ` `1` `)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Python implementation of the approach ` ` ` `# Recursive function that returns ` `# the count of permutation-based ` `# on the length of the array. ` ` ` `def` `count(n): ` ` ` `if` `n ` `=` `=` `0` `: ` ` ` `return` `1` ` ` `if` `n ` `=` `=` `1` `: ` ` ` `return` `1` ` ` `else` `: ` ` ` `return` `(n ` `*` `count(n` `-` `1` `)) ` `+` `((n` `-` `1` `) ` `*` `count(n` `-` `2` `)) ` ` ` `# Driver Code ` `A ` `=` `[` `1` `, ` `2` `, ` `3` `, ` `9` `] ` `print` `(count(` `len` `(A)` `-` `1` `)) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Recursive function that returns ` `// the count of permutation-based ` `// on the length of the array. ` `static` `int` `count(` `int` `n) ` `{ ` ` ` `if` `(n == 0) ` ` ` `return` `1; ` ` ` `if` `(n == 1) ` ` ` `return` `1; ` ` ` `else` ` ` `return` `(n * count(n - 1)) + ` ` ` `((n - 1) * count(n - 2)); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]A = {1, 2, 3, 9}; ` ` ` ` ` `// length of array ` ` ` `int` `n = 4; ` ` ` ` ` `// Output required answer ` ` ` `Console.WriteLine(count(n - 1)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count subarrays having total distinct elements same as original array
- Find original array from encrypted array (An array of sums of other elements)
- Maximum count number of valley elements in a subarray of size K
- Find a subarray whose sum is divisible by size of the array
- Create an array of size N with sum S such that no subarray exists with sum S or S-K
- Smallest subarray whose sum is multiple of array size
- Minimum number of operations to convert array A to array B by adding an integer into a subarray
- Find array elements equal to sum of any subarray of at least size 2
- Generate original array from an array that store the counts of greater elements on right
- Count of elements which is the sum of a subarray of the given Array
- Count of Subarrays in an array containing numbers from 1 to the length of subarray
- Count Inversions of size three in a given array
- Count of subarrays of size K which is a permutation of numbers from 1 to K
- Count the Arithmetic sequences in the Array of size at least 3
- Count the number of primes in the prefix sum array of the given array
- Minimum count of increment of K size subarrays required to form a given Array
- Number of triplets in array having subarray xor equal
- Check in binary array the number represented by a subarray is odd or even
- Permutation of an array that has smaller values from another array
- Find permutation array from the cumulative sum array

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.