# A permutation where each element indicates either number of elements before or after it

• Difficulty Level : Easy
• Last Updated : 09 Mar, 2022

Given an array of n elements. The task is to check whether a permutation of given array exists, such that each element indicate number of element present before or after it. Print “Yes” if exists, else print “No”.
Examples :

```Input : arr[] = {1, 3, 3, 2}
Output : Yes
{3, 1, 2, 3} is a permutation in which each element
indicate number of element present before or after it.
There is one more permutation {3, 2, 1, 3}

Input : arr[] = {4, 1, 2, 3, 0}
Output : Yes
There are two permutations {0, 1, 2, 3, 4} or
{4, 3, 2, 1, 0}```

The idea is to use hashing. Observe, for each index i in the array, arr[i] can have value i or n – i. We traverse the given array and find the frequency of each element present in the array. Now, for each index i, check availability of value i and n-i and accordingly decrement the frequency. Note that an item with value i can either go to index i or n-i-1. Similarly, an item with value n-i-1 can go to either index i or n-i-1.
Below is the implementation of this approach:

## C++

 `// C++ program to check if array permutation``// exists such that each element indicates``// either number of elements before or after``// it.``#include ``using` `namespace` `std;` `// Check if array permutation exist such that``// each element indicate either number of``// elements before or after it.``bool` `check(``int` `arr[], ``int` `n)``{``    ``map<``int``, ``int``> freq;` `    ``// Finding the frequency of each number.``    ``for` `(``int` `i = 0; i < n; i++)``        ``freq[arr[i]]++;` `    ``for` `(``int` `i = 0; i < n; i++)``    ``{``        ``// Try to find number of element before``        ``// the current index.``        ``if` `(freq[i])``            ``freq[i]--;` `        ``// Try to find number of element after``        ``// the current index.``        ``else` `if` `(freq[n-i-1])``            ``freq[n-i-1]--;` `        ``// If no such number find, return false.``        ``else``            ``return` `false``;``    ``}` `    ``return` `true``;``}` `// Driven Program``int` `main()``{``    ``int` `arr[] = {1, 3, 3, 2};``    ``int` `n = ``sizeof``(arr)/``sizeof``(arr);` `    ``check(arr, n)? (cout << ``"Yes"` `<< endl) :``                   ``(cout << ``"No"` `<< endl);` `    ``return` `0;``}`

## Java

 `// Java program to check if array permutation``// exists such that each element indicates``// either number of elements before or after``// it.``import` `java.io.*;``class` `GFG``{``    ``// Check if array permutation exist such that``    ``// each element indicate either number of``    ``// elements before or after it.``    ``public` `static` `boolean` `check(``int` `arr[])``    ``{``        ``int` `n = arr.length;``        ``int``[] freq = ``new` `int``[n];`` ` `        ``// Finding the frequency of each number.``        ``for` `(``int` `i = ``0``; i < n; i++)``            ``freq[arr[i]]++;`` ` `        ``for` `(``int` `i = ``0``; i < n; i++)``        ``{``            ``// Try to find number of element before``            ``// the current index.``            ``if` `(freq[i]!= ``0``)``                 ``freq[i]--;`` ` `            ``// Try to find number of element after``            ``// the current index.``            ``else` `if` `(freq[n-i-``1``]!= ``0``)``                    ``freq[n-i-``1``]--;`` ` `            ``// If no such number find, return false.``            ``else``                ``return` `false``;``        ``}`` ` `    ``return` `true``;``    ``}``    ` `    ``//Driver program``    ``public` `static` `void` `main (String[] args)``    ``{``        ` `        ``int` `arr[] = {``1``, ``3``, ``3``, ``2``};``        ``boolean` `bool = check(arr);``        ``if``(bool)``            ``System.out.println(``"Yes"``);``        ``else``            ``System.out.println(``"No"``);``    ``}``}`

## Python3

 `# Python program to check if array permutation``# exists such that each element indicates``# either number of elements before or after``# it.` `# Check if array permutation exist such that``# each element indicate either number of``# elements before or after it.``def` `check(arr):` `    ``n ``=` `len``(arr);``    ``freq ``=` `[``0``] ``*` `n;` `    ``# Finding the frequency of each number.``    ``for` `i ``in` `range``(n):``        ``freq[arr[i]] ``+``=` `1``;` `    ``for` `i ``in` `range``(n):``        ` `        ``# Try to find number of element before``        ``# the current index.``        ``if` `(freq[i] !``=` `0``):``            ``freq[i] ``-``=` `1``;` `        ``# Try to find number of element after``        ``# the current index.``        ``else` `if` `(freq[n ``-` `i ``-` `1``] !``=` `0``):``                ``freq[n ``-` `i ``-` `1``] ``-``=` `1``;` `        ``# If no such number find, return false.``        ``else``:``            ``return` `False``;` `    ``return` `True``;``    ` `# Driver program``if` `__name__ ``=``=` `'__main__'``:` `    ``arr ``=` `[``1``, ``3``, ``3``, ``2``];``    ``bool` `=` `check(arr);``    ``if``(``bool``):``        ``print``(``"Yes"``);``    ``else``:``        ``print``(``"No"``);` `# This code is contributed by 29AjayKumar`

## C#

 `// C# program to check if array permutation``// exists such that each element indicates``// either number of elements before or after it.``using` `System;` `class` `GFG``{``    ``// Check if array permutation exist such that``    ``// each element indicate either number of``    ``// elements before or after it.``    ``public` `static` `bool` `check(``int` `[]arr)``    ``{``        ``int` `n = arr.Length;``        ``int` `[]freq = ``new` `int``[n];` `        ``// Finding the frequency of each number.``        ``for` `(``int` `i = 0; i < n; i++)``            ``freq[arr[i]]++;` `        ``for` `(``int` `i = 0; i < n; i++)``        ``{``            ``// Try to find number of element``            ``// before the current index.``            ``if` `(freq[i]!= 0)``                ``freq[i]--;` `            ``// Try to find number of element``            ``// after the current index.``            ``else` `if` `(freq[n-i-1] != 0)``                    ``freq[n-i-1]--;` `            ``// If no such number find, return false.``            ``else``                ``return` `false``;``        ``}` `    ``return` `true``;``    ``}``    ` `    ``//Driver program``    ``public` `static` `void` `Main ()``    ``{``        ``int` `[]arr = {1, 3, 3, 2};``        ``bool` `boo = check(arr);``        ``if``(boo)``            ``Console.Write(``"Yes"``);``        ``else``            ``Console.Write(``"No"``);``    ``}``}` `// This code is contributed by nitin mittal.`

## Javascript

 ``

Output:

`Yes`

Time Complexity: O(n).
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.