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++
#include <bits/stdc++.h>
using namespace std;
bool check( int arr[], int n)
{
map< int , int > freq;
for ( int i = 0; i < n; i++)
freq[arr[i]]++;
for ( int i = 0; i < n; i++)
{
if (freq[i])
freq[i]--;
else if (freq[n-i-1])
freq[n-i-1]--;
else
return false ;
}
return true ;
}
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
import java.io.*;
class GFG
{
public static boolean check( int arr[])
{
int n = arr.length;
int [] freq = new int [n];
for ( int i = 0 ; i < n; i++)
freq[arr[i]]++;
for ( int i = 0 ; i < n; i++)
{
if (freq[i]!= 0 )
freq[i]--;
else if (freq[n-i- 1 ]!= 0 )
freq[n-i- 1 ]--;
else
return false ;
}
return true ;
}
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
def check(arr):
n = len (arr);
freq = [ 0 ] * n;
for i in range (n):
freq[arr[i]] + = 1 ;
for i in range (n):
if (freq[i] ! = 0 ):
freq[i] - = 1 ;
else if (freq[n - i - 1 ] ! = 0 ):
freq[n - i - 1 ] - = 1 ;
else :
return False ;
return True ;
if __name__ = = '__main__' :
arr = [ 1 , 3 , 3 , 2 ];
bool = check(arr);
if ( bool ):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
public static bool check( int []arr)
{
int n = arr.Length;
int []freq = new int [n];
for ( int i = 0; i < n; i++)
freq[arr[i]]++;
for ( int i = 0; i < n; i++)
{
if (freq[i]!= 0)
freq[i]--;
else if (freq[n-i-1] != 0)
freq[n-i-1]--;
else
return false ;
}
return true ;
}
public static void Main ()
{
int []arr = {1, 3, 3, 2};
bool boo = check(arr);
if (boo)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function check(arr)
{
var n = arr.length;
var freq = new Array(n).fill(0);
for ( var i = 0; i < n; i++) freq[arr[i]]++;
for ( var i = 0; i < n; i++)
{
if (freq[i] !== 0) freq[i]--;
else if (freq[n - i - 1] !== 0) freq[n - i - 1]--;
else return false ;
}
return true ;
}
var arr = [1, 3, 3, 2];
var boo = check(arr);
if (boo) document.write( "Yes" );
else document.write( "No" );
</script>
|
Time Complexity: O(n).
Last Updated :
13 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...