Find the Number of Permutations that satisfy the given condition in an array

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 decsending 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 decsending 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:



  1. 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.
  2. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the number
// of permutations that satisfy
// the given condition in an array
#include <bits/stdc++.h>
using namespace std;
  
// Function to calculate x ^ y
// recursively
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;
}
  
// Function to return the number of
// permutations that satisfy the
// given condition in an array
int noOfPermutations(int* a, int n)
{
    // If there is only one element then
    // only one permutation is available
    if (n == 1) {
        return 1;
    }
  
    // Sort the array for calculating
    // the number of elements occurring twice
    sort(a, a + n);
  
    // If the maximum element is occurring
    // twice, then the number of permutations
    // satisfying the condition is 0
    if (a[n - 1] == a[n - 2]) {
        return 0;
    }
  
    // This variable will store the
    // number of element occurring twice
    int x = 0;
  
    // Loop to check the number of elements
    // occurring twice
    for (int i = 0; i < n - 2; ++i) {
  
        // Check if this element
        // is occurring twice
        if (a[i] == a[i + 1]) {
  
            // If this element is occurring
            // twice then check if this number
            // is occurring more than twice
            if (a[i] == a[i + 2]) {
  
                // If element occurring thrice
                // then no permutation will
                // satisfy the given condition
                return 0;
            }
  
            x++;
  
            // Since we have checked the next
            // element as well, then we can
            // increment the loop variable
            i++;
        }
    }
  
    return pow(2, n - 2 * x - 1);
}
  
// Driver code
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the number
// of permutations that satisfy
// the given condition in an array
  
import java.util.*;
  
class GFG{
   
// Function to calculate x ^ y
// recursively
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;
}
   
// Function to return the number of
// permutations that satisfy the
// given condition in an array
static int noOfPermutations(int []a, int n)
{
    // If there is only one element then
    // only one permutation is available
    if (n == 1) {
        return 1;
    }
   
    // Sort the array for calculating
    // the number of elements occurring twice
    Arrays.sort(a);
   
    // If the maximum element is occurring
    // twice, then the number of permutations
    // satisfying the condition is 0
    if (a[n - 1] == a[n - 2]) {
        return 0;
    }
   
    // This variable will store the
    // number of element occurring twice
    int x = 0;
   
    // Loop to check the number of elements
    // occurring twice
    for (int i = 0; i < n - 2; ++i) {
   
        // Check if this element
        // is occurring twice
        if (a[i] == a[i + 1]) {
   
            // If this element is occurring
            // twice then check if this number
            // is occurring more than twice
            if (a[i] == a[i + 2]) {
   
                // If element occurring thrice
                // then no permutation will
                // satisfy the given condition
                return 0;
            }
   
            x++;
   
            // Since we have checked the next
            // element as well, then we can
            // increment the loop variable
            i++;
        }
    }
   
    return pow(2, n - 2 * x - 1);
}
   
// Driver code
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);
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find the number
# of permutations that satisfy
# the given condition in an array
  
# Function to calculate x ^ y
# recursively
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
  
# Function to return the number of
# permutations that satisfy the
# given condition in an array
def noOfPermutations(a, n):
  
    # If there is only one element then
    # only one permutation is available
    if (n == 1):
        return 1
  
    # Sort the array for calculating
    # the number of elements occurring twice
    a.sort()
  
    # If the maximum element is occurring
    # twice, then the number of permutations
    # satisfying the condition is 0
    if (a[n - 1] == a[n - 2]):
        return 0
  
    # This variable will store the
    # number of element occurring twice
    x = 0
  
    # Loop to check the number of elements
    # occurring twice
    for i in range( n - 2):
  
        # Check if this element
        # is occurring twice
        if (a[i] == a[i + 1]):
  
            # If this element is occurring
            # twice then check if this number
            # is occurring more than twice
            if (a[i] == a[i + 2]):
  
                # If element occurring thrice
                # then no permutation will
                # satisfy the given condition
                return 0
          
            x += 1
  
            # Since we have checked the next
            # element as well, then we can
            # increment the loop variable
            i += 1
  
    return pow(2, n - 2 * x - 1)
  
# Driver code
if __name__ == "__main__":
  
    a = [ 1, 2, 2, 3, 4 ]
    n = len(a)
    num = noOfPermutations(a, n)
    print (num)
  
# This code is contributed by chitranayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the number
// of permutations that satisfy
// the given condition in an array 
using System;
  
class GFG{
    
// Function to calculate x ^ y
// recursively
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;
}
    
// Function to return the number of
// permutations that satisfy the
// given condition in an array
static int noOfPermutations(int []a, int n)
{
    // If there is only one element then
    // only one permutation is available
    if (n == 1) {
        return 1;
    }
    
    // Sort the array for calculating
    // the number of elements occurring twice
    Array.Sort(a);
    
    // If the maximum element is occurring
    // twice, then the number of permutations
    // satisfying the condition is 0
    if (a[n - 1] == a[n - 2]) {
        return 0;
    }
    
    // This variable will store the
    // number of element occurring twice
    int x = 0;
    
    // Loop to check the number of elements
    // occurring twice
    for (int i = 0; i < n - 2; ++i) {
    
        // Check if this element
        // is occurring twice
        if (a[i] == a[i + 1]) {
    
            // If this element is occurring
            // twice then check if this number
            // is occurring more than twice
            if (a[i] == a[i + 2]) {
    
                // If element occurring thrice
                // then no permutation will
                // satisfy the given condition
                return 0;
            }
    
            x++;
    
            // Since we have checked the next
            // element as well, then we can
            // increment the loop variable
            i++;
        }
    }
    
    return pow(2, n - 2 * x - 1);
}
    
// Driver code
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);
}
}
   
// This code is contributed by 29AjayKumar

chevron_right


Output:

4

Time Complexity: O(N * log(N))

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.