# Check if an array can be formed by merging 2 non-empty permutations

Given an array **arr[]** of length **N**, the task is to check if it can be formed by merging two permutations of same or different length. Print **YES** if such merging is possible. Otherwise, print **NO**.

Permutations of length 3 are {1, 2, 3}, {2, 3, 1}, {1, 3, 2}, {3, 1, 2}, {3, 2, 1}, {2, 1, 3}.

**Examples:**

Input:arr = [1, 3, 2, 4, 3, 1, 2]

Output:YES

Explanation:

The given array can be formed by merging a permutation of length 4 [1, 3, 2, 4] and permutation of length 3 [3, 1, 2]

Input:arr = [1, 2, 3, 2, 3, 2, 1]

Output:NO

**Approach :**

We can observe that minimum excludant (MEX) of a permutation of length **N** is **N+1**.

So, if the length of the first permutation is **l**, then MEX of the prefix **arr [0 …… l-1]** is **l+1** and the MEX of the suffix **a[l …… n]** will be **N – l + 1**.

So, we can calculate MEX of prefix and suffixes and if the above condition is satisfied, the answer will be **“YES”**. Otherwise, answer will be **“NO”**.

Below is the implementation of the above approach:

## Python3

`def` `if_merged_permutations(a, n): ` ` ` `pre_mex ` `=` `[` `1` `for` `i ` `in` `range` `(n)] ` ` ` ` ` `# Calculate the mex of the ` ` ` `# array a[0...i] ` ` ` `freq ` `=` `[` `0` `for` `i ` `in` `range` `(n ` `+` `1` `)] ` ` ` ` ` `# Mex of empty array is 1 ` ` ` `mex ` `=` `1` ` ` ` ` `# Calculating the frequency ` ` ` `# of array elements ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `freq[a[i]]` `+` `=` `1` ` ` `if` `freq[a[i]]>` `1` `: ` ` ` `# In a permutation ` ` ` `# each element is ` ` ` `# present one time, ` ` ` `# So there is no chance ` ` ` `# of getting permutations ` ` ` `# for the prefix of ` ` ` `# length greater than i ` ` ` `break` ` ` ` ` `# The current element ` ` ` `# is the mex ` ` ` `if` `a[i]` `=` `=` `mex: ` ` ` ` ` `# While mex is present ` ` ` `# in the array ` ` ` `while` `freq[mex]!` `=` `0` `: ` ` ` `mex` `+` `=` `1` ` ` `pre_mex[i]` `=` `mex ` ` ` ` ` `suf_mex ` `=` `[` `1` `for` `i ` `in` `range` `(n)] ` ` ` ` ` `# Calculate the mex of the ` ` ` `# array a[i..n] ` ` ` `freq ` `=` `[` `0` `for` `i ` `in` `range` `(n ` `+` `1` `)] ` ` ` ` ` `# Mex of empty array is 1 ` ` ` `mex ` `=` `1` ` ` ` ` `# Calculating the frequency ` ` ` `# of array elements ` ` ` `for` `i ` `in` `range` `(n` `-` `1` `, ` `-` `1` `, ` `-` `1` `): ` ` ` `freq[a[i]]` `+` `=` `1` ` ` `if` `freq[a[i]]>` `1` `: ` ` ` ` ` `# In a permutation each ` ` ` `# element is present ` ` ` `# one time, So there is ` ` ` `# no chance of getting ` ` ` `# permutations for the ` ` ` `# suffix of length lesser ` ` ` `# than i ` ` ` `break` ` ` ` ` `# The current element is ` ` ` `# the mex ` ` ` `if` `a[i]` `=` `=` `mex: ` ` ` `# While mex is present ` ` ` `# in the array ` ` ` `while` `freq[mex]!` `=` `0` `: ` ` ` `mex` `+` `=` `1` ` ` `suf_mex[i]` `=` `mex ` ` ` ` ` `# Now check if there is atleast ` ` ` `# one index i such that mex of ` ` ` `# the prefix a[0..i]= i + ` ` ` `# 2(0 based indexing) and mex ` ` ` `# of the suffix a[i + 1..n]= n-i ` ` ` ` ` `for` `i ` `in` `range` `(n` `-` `1` `): ` ` ` `if` `pre_mex[i]` `=` `=` `i ` `+` `2` `and` `suf_mex[i ` `+` `1` `]` `=` `=` `n` `-` `i: ` ` ` `print` `(` `"YES"` `) ` ` ` `return` ` ` `print` `(` `"NO"` `) ` ` ` `a ` `=` `[` `1` `, ` `3` `, ` `2` `, ` `4` `, ` `3` `, ` `1` `, ` `2` `] ` `n ` `=` `len` `(a) ` `if_merged_permutations(a, n) ` |

*chevron_right*

*filter_none*

**Output:**

YES

**Time Complexity:** *O(N)*

## Recommended Posts:

- Check if given string can be formed by two other strings or their permutations
- Check whether an Array can be made 0 by splitting and merging repeatedly
- Sum of all numbers that can be formed with permutations of n digits
- Check whether Arithmetic Progression can be formed from the given array
- Stack Permutations (Check if an array is stack permutation of other)
- Maximum value after merging all elements in the array
- Merging elements of two different arrays alternatively in third array
- Modify array by merging elements with addition such that it consists of only Primes.
- Check if two arrays are permutations of each other
- Minimum cost of reducing Array by merging any adjacent elements repetitively
- Check if a Sequence is a concatenation of two permutations
- Check if a binary string contains all permutations of length k
- Check if two arrays are permutations of each other using Mathematical Operation
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Print k different sorted permutations of a given array
- Find Kth number from sorted array formed by multiplying any two numbers in the array
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Check if a number is formed by Concatenation of 1, 14 or 144 only
- Maximum subarray sum in array formed by repeating the given array k times
- Iterative approach to print all permutations of an 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.