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++ 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"); } } |
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. |
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.
Recommended Posts:
- Given an array A[] and a number x, check for pair in A[] with sum as x
- Majority Element
- Find the Number Occurring Odd Number of Times
- Find the Missing Number
- Search an element in a sorted and rotated array
- Sort elements by frequency | Set 1
- k largest(or smallest) elements in an array | added Min Heap method
- Maximum sum such that no two elements are adjacent
- Two elements whose sum is closest to zero
- Find the smallest and second smallest elements in an array
- Maximum and minimum of an array using minimum number of comparisons
- Check for Majority Element in a sorted array
- Maximum difference between two elements such that larger element appears after the smaller number
- Find the two repeating elements in a given array
- Next Greater Element
Improved By : nitin mittal

