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

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++

// C++ program to check if array permutation
// exists such that each element indicates
// either number of elements before or after
// it.
#include <bits/stdc++.h>
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[0]);

    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");
	}
}


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 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.

GATE CS Corner    Company Wise Coding Practice





Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.