Given an array arr[] of size N, the task is to find the number of permutations in the array that follows the given condition:
- If K is the maximum element in the array, then the elements before K in the array should be in the ascending order and the elements after K in the array should be in the descending order.
Examples:
Input: arr[] = {1, 2, 3}
Output: 4
Explanation:
There are a total of 6 permutations for the given array {1, 2, 3}. They are:
{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, and {3, 2, 1}
Out of the above permutations, only {1, 2, 3}, {1, 3, 2}, {2, 3, 1}, {3, 2, 1} are the arrays which follow the strictly ascending order before the maximum element 3 and strictly descending order after it.
The permutations which do not satisfy this condition are {2, 1, 3}, {3, 1, 2}.
Input: arr[] = {1 1 2}
Output: 1
There are a total of 3 permutations for the given array {1, 1, 2}. They are:
{1 1 2}, {1 2 1} and {2 1 1}
Out of the above permutations, only {1, 2, 1} is the array which follow the strictly ascending order before the maximum element 2 and strictly descending order after it.
The permutations which do not satisfy this condition are {1, 1, 2}, {2, 2, 1}.
Observations: On observing carefully, the following observations can be made:
- It can be concluded that if any number repeats more than twice, then there will be no permutation which satisfies the given condition. This is because, in all the permutations, this will be seen twice either before the maximum element or after the maximum element thus violating the given condition.
- The second observation which can be made is that the maximum element in the array should appear only once. If it appears more than once, then the extra copies may be seen before the maximum element thereby violating the given condition.
Approach: When the above two observations are not violated, then the idea is to partition the array into two parts and fill elements in each partition as:
- Since we can partition the array into two parts. One is before the maximum element and the other one is after the maximum element. Therefore, every element has two choices whether to appear either before the maximum element or after the maximum element except the maximum element in the array.
- If any element appears twice in the array, then that element has only one option. It definitely has to appear once before the maximum element and once after the maximum element.
- For example,
- If arr[] = {1, 2, 2, 3, 4}, the maximum element 4 has only one occurrence and no element occurs more than twice.
- Now, the array can be divided into two parts: { }4{ } with 4 being the maximum element.
- Since 2 is repeated twice, it should be available on both the sides (i.e.) {2}4{2}.
- 1 and 3 both have two choices for the left part and right part. Therefore, there are 2 * 2 = 4 possible permutations.
- Therefore, if N is the size of the array and M is the number of elements in the array with its occurrence = 2, then the number of permutations satisfying the condition will be 2(N – (2 * X) – 1).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int pow ( int x, int y)
{
if (y == 1)
return x;
if (y == 0)
return 1;
int temp = pow (x, y / 2);
temp *= temp;
if (y & 1)
temp *= x;
return temp;
}
int noOfPermutations( int * a, int n)
{
if (n == 1) {
return 1;
}
sort(a, a + n);
if (a[n - 1] == a[n - 2]) {
return 0;
}
int x = 0;
for ( int i = 0; i < n - 2; ++i) {
if (a[i] == a[i + 1]) {
if (a[i] == a[i + 2]) {
return 0;
}
x++;
i++;
}
}
return pow (2, n - 2 * x - 1);
}
int main()
{
int a[] = { 1, 2, 2, 3, 4 };
int n = sizeof (a) / sizeof (a[0]);
int num = noOfPermutations(a, n);
cout << num;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int pow( int x, int y)
{
if (y == 1 )
return x;
if (y == 0 )
return 1 ;
int temp = pow(x, y / 2 );
temp *= temp;
if (y % 2 == 1 )
temp *= x;
return temp;
}
static int noOfPermutations( int []a, int n)
{
if (n == 1 ) {
return 1 ;
}
Arrays.sort(a);
if (a[n - 1 ] == a[n - 2 ]) {
return 0 ;
}
int x = 0 ;
for ( int i = 0 ; i < n - 2 ; ++i) {
if (a[i] == a[i + 1 ]) {
if (a[i] == a[i + 2 ]) {
return 0 ;
}
x++;
i++;
}
}
return pow( 2 , n - 2 * x - 1 );
}
public static void main(String[] args)
{
int a[] = { 1 , 2 , 2 , 3 , 4 };
int n = a.length;
int num = noOfPermutations(a, n);
System.out.print(num);
}
}
|
Python 3
def pow ( x, y):
if (y = = 1 ):
return x
if (y = = 0 ):
return 1
temp = pow (x, y / / 2 )
temp * = temp
if (y & 1 ):
temp * = x
return temp
def noOfPermutations(a, n):
if (n = = 1 ):
return 1
a.sort()
if (a[n - 1 ] = = a[n - 2 ]):
return 0
x = 0
for i in range ( n - 2 ):
if (a[i] = = a[i + 1 ]):
if (a[i] = = a[i + 2 ]):
return 0
x + = 1
i + = 1
return pow ( 2 , n - 2 * x - 1 )
if __name__ = = "__main__" :
a = [ 1 , 2 , 2 , 3 , 4 ]
n = len (a)
num = noOfPermutations(a, n)
print (num)
|
C#
using System;
class GFG{
static int pow( int x, int y)
{
if (y == 1)
return x;
if (y == 0)
return 1;
int temp = pow(x, y / 2);
temp *= temp;
if (y % 2 == 1)
temp *= x;
return temp;
}
static int noOfPermutations( int []a, int n)
{
if (n == 1) {
return 1;
}
Array.Sort(a);
if (a[n - 1] == a[n - 2]) {
return 0;
}
int x = 0;
for ( int i = 0; i < n - 2; ++i) {
if (a[i] == a[i + 1]) {
if (a[i] == a[i + 2]) {
return 0;
}
x++;
i++;
}
}
return pow(2, n - 2 * x - 1);
}
public static void Main(String[] args)
{
int []a = { 1, 2, 2, 3, 4 };
int n = a.Length;
int num = noOfPermutations(a, n);
Console.Write(num);
}
}
|
Javascript
<script>
function pow(x, y)
{
if (y == 1)
return x;
if (y == 0)
return 1;
let temp = pow(x, y / 2);
temp *= temp;
if (y % 2 == 1)
temp *= x;
return temp;
}
function noOfPermutations(a, n)
{
if (n == 1) {
return 1;
}
a.sort();
if (a[n - 1] == a[n - 2]) {
return 0;
}
let x = 0;
for (let i = 0; i < n - 2; ++i) {
if (a[i] == a[i + 1]) {
if (a[i] == a[i + 2]) {
return 0;
}
x++;
i++;
}
}
return pow(2, n - 2 * x - 1);
}
let a = [ 1, 2, 2, 3, 4 ];
let n = a.length;
let num = noOfPermutations(a, n);
document.write(num);
</script>
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(log y)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
21 Nov, 2021
Like Article
Save Article