- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
A permutation where each element indicates either number of elements before or after it?
In this section we will see one problem. Here n elements are given in an array. We have to check whether there is a permutation of that array exists, such that each element indicates the number of elements present either before or after it.
Suppose the array elements are {2, 1, 3, 3}. The appropriate permutation is like {3, 1, 2, 3}. Here the first 3 is indicating there are three elements next of it, the 1 indicates there is only one element before this. The 2 indicates there are two elements before it and the last 3 indicates that there are three elements before it.
Algorithm
checkPermutation(arr, n)
begin define a hashmap to hold frequencies. The key and value are of integer type of the map. for each element e in arr, do increase map[e] by 1 done for i := 0 to n-1, do if map[i] is non-zero, then decrease map[i] by 1 else if map[n-i-1] is non-zero, then decrease map[n-i-1] by 1 else return false end if done return true end
Example
#include<iostream> #include<map> using namespace std; bool checkPermutation(int arr[], int n) { map<int, int> freq_map; for(int i = 0; i < n; i++){ //get the frequency of each number freq_map[arr[i]]++; } for(int i = 0; i < n; i++){ if(freq_map[i]){ //count number of elements before current element freq_map[i]--; } else if(freq_map[n-i-1]){ //count number of elements after current element freq_map[n-i-1]--; } else { return false; } } return true; } main() { int data[] = {3, 2, 3, 1}; int n = sizeof(data)/sizeof(data[0]); if(checkPermutation(data, n)){ cout << "Permutation is present"; } else { cout << "Permutation is not present"; } }
Output
Permutation is present
- Related Articles
- Find the element before which all the elements are smaller than it, and after which all are greater in Python
- The ::before and ::after Pseudo-element in CSS
- Is MySQL LIMIT applied before or after ORDER BY?
- Write a program in Python to verify kth index element is either alphabet or number in a given series
- Complete the following sentences:Every real number is either… number or… number.
- How to manipulate CSS pseudo-elements ::before and ::after using jQuery?
- Complete the following sentences:The decimal representation of a rational number is either… or…
- How to save list where each element contains equal number of values to a text file in R?
- Select MongoDB documents where a field either does not exist, is null, or is false?
- How to create permutation of array with the given number of elements in JavaScript
- Selects all elements where the parent is a element with CSS
- When sunlight is concentrated on a piece of paper by a spherical mirror or lens, then a hole can be burnt in it. For doing this, the paper must be placed at he focus of:(a) either a convex mirror or convex lens (b) either a concave mirror or concave lens(c) either a concave mirror or convex lens (d) either a convex mirror or concave lens
- How to see if a date is before or after another date in Java 8?
- Maximum difference between two elements such that larger element appears after the smaller number in C
- JavaScript: Computing the average of an array after mapping each element to a value

Advertisements