We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The number of beautiful permutations = number of total permutations - number of ugly permutations.
An ugly permutation is one that violates the condition fora beautiful permutation, i.e. it contains two identical numbers next to each other.
To calculate the number of ugly permutations:
1. Calculate the number of pairs of identical elements present in the input array.
2. Choose a particular pair.
3. Now place this pair in each of the positions in the array and permutate the rest of the array with the remaining elements.
4. Repeat steps 2&3. above for all possible pairs of identical elements.
5. Take care of repititions.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Tara's Beautiful Permutations
You are viewing a single comment's thread. Return to all comments →
The number of beautiful permutations = number of total permutations - number of ugly permutations.
An ugly permutation is one that violates the condition fora beautiful permutation, i.e. it contains two identical numbers next to each other.
To calculate the number of ugly permutations: 1. Calculate the number of pairs of identical elements present in the input array. 2. Choose a particular pair. 3. Now place this pair in each of the positions in the array and permutate the rest of the array with the remaining elements. 4. Repeat steps 2&3. above for all possible pairs of identical elements. 5. Take care of repititions.