Skip to content
Related Articles

Related Articles

Improve Article

Count possible permutations of given array satisfying the given conditions

  • Last Updated : 27 Apr, 2021

Given an array, arr[] consisting of N distinct elements, the task is to count possible permutations of the given array that can be generated which satisfies the following properties: 

  • The two halves must be sorted.
  • arr[i] must be less than arr[N / 2 + i]

Note: N is always even and indexing starts from 0.

Examples:

Input: arr[] = {10, 20, 30, 40} 
Output:
Explanation: 
Possible permutations of the given array that satisfy the given conditions are:{{10, 20, 30, 40}, {10, 30, 20, 40}}. 
Therefore, the required output is 2. 

Input: arr[] = {1, 2}
Output: 1

Approach: Follow the steps below to solve the problem: 



  • Initialize a variable, say cntPerm to store the count of permutations of the given array that satisfy the given condition.
  • Find the value of the binomial coefficient of 2NCN using the following formula:

\binom{N}{R} = [{N × (N – 1) × …………. × (N – R + 1)} / {(R × (R – 1) × ….. × 1)}]  

  • Finally, calculate catalan number = 2NCN / (N + 1) and print it as the required answer.

Below is the implementation of the above approach: 

C++




// C++ Program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to get the value
// of binomial coefficient
int binCoff(int N, int R)
{
    // Stores the value of
    // binomial coefficient
    int res = 1;
 
    if (R > (N - R)) {
 
        // Since C(N, R)
        // = C(N, N - R)
        R = (N - R);
    }
 
    // Calculate the value
    // of C(N, R)
    for (int i = 0; i < R; i++) {
        res *= (N - i);
        res /= (i + 1);
    }
    return res;
}
 
// Function to get the count of
// permutations of the array
// that satisfy the condition
int cntPermutation(int N)
{
    // Stores count of permutations
    // of the array that satisfy
    // the given condition
    int cntPerm;
 
    // Stores the value of C(2N, N)
    int C_2N_N = binCoff(2 * N, N);
 
    // Stores the value of
    // catalan number
    cntPerm = C_2N_N / (N + 1);
 
    // Return answer
    return cntPerm;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 1, 2, 3, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << cntPermutation(N / 2);
 
    return 0;
}

Java




// Java Program to implement
// the above approach
import java.io.*;
class GFG{
  
// Function to get the value
// of binomial coefficient
static int binCoff(int N,
                   int R)
{
  // Stores the value of
  // binomial coefficient
  int res = 1;
 
  if (R > (N - R))
  {
    // Since C(N, R)
    // = C(N, N - R)
    R = (N - R);
  }
 
  // Calculate the value
  // of C(N, R)
  for (int i = 0; i < R; i++)
  {
    res *= (N - i);
    res /= (i + 1);
  }
  return res;
}
 
// Function to get the count of
// permutations of the array
// that satisfy the condition
static int cntPermutation(int N)
{
  // Stores count of permutations
  // of the array that satisfy
  // the given condition
  int cntPerm;
 
  // Stores the value of C(2N, N)
  int C_2N_N = binCoff(2 * N, N);
 
  // Stores the value of
  // catalan number
  cntPerm = C_2N_N / (N + 1);
 
  // Return answer
  return cntPerm;
}
  
// Driver Code
public static void main (String[] args)
{
  int arr[] = {1, 2, 3, 4};
  int N = arr.length;
  System.out.println(cntPermutation(N / 2));
}
}
 
// This code is contributed by sanjoy_62

Python3




# Python3 program to implement
# the above approach
 
# Function to get the value
# of binomial coefficient
def binCoff(N, R):
     
    # Stores the value of
    # binomial coefficient
    res = 1
 
    if (R > (N - R)):
 
        # Since C(N, R)
        # = C(N, N - R)
        R = (N - R)
 
    # Calculate the value
    # of C(N, R)
    for i in range(R):
        res *= (N - i)
        res //= (i + 1)
 
    return res
 
# Function to get the count of
# permutations of the array
# that satisfy the condition
def cntPermutation(N):
     
    # Stores count of permutations
    # of the array that satisfy
    # the given condition
 
    # Stores the value of C(2N, N)
    C_2N_N = binCoff(2 * N, N)
 
    # Stores the value of
    # catalan number
    cntPerm = C_2N_N // (N + 1)
 
    # Return answer
    return cntPerm
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 2, 3, 4 ]
    N = len(arr)
     
    print(cntPermutation(N // 2))
 
# This code is contributed by mohit kumar 29

C#




// C# Program to implement
// the above approach
using System;
class GFG{
  
// Function to get the value
// of binomial coefficient
static int binCoff(int N,
                   int R)
{
  // Stores the value of
  // binomial coefficient
  int res = 1;
 
  if (R > (N - R))
  {
    // Since C(N, R)
    // = C(N, N - R)
    R = (N - R);
  }
 
  // Calculate the value
  // of C(N, R)
  for (int i = 0; i < R; i++)
  {
    res *= (N - i);
    res /= (i + 1);
  }
  return res;
}
 
// Function to get the count of
// permutations of the array
// that satisfy the condition
static int cntPermutation(int N)
{
  // Stores count of permutations
  // of the array that satisfy
  // the given condition
  int cntPerm;
 
  // Stores the value of C(2N, N)
  int C_2N_N = binCoff(2 * N, N);
 
  // Stores the value of
  // catalan number
  cntPerm = C_2N_N / (N + 1);
 
  // Return answer
  return cntPerm;
}
  
// Driver Code
public static void Main(String[] args)
{
  int []arr = {1, 2, 3, 4};
  int N = arr.Length;
  Console.WriteLine(cntPermutation(N / 2));
}
}
 
// This code is contributed by shikhasingrajput

Javascript




<script>
// javascript Program to implement
// the above approach
 
    // Function to get the value
    // of binomial coefficient
    function binCoff(N , R) {
        // Stores the value of
        // binomial coefficient
        var res = 1;
 
        if (R > (N - R)) {
            // Since C(N, R)
            // = C(N, N - R)
            R = (N - R);
        }
 
        // Calculate the value
        // of C(N, R)
        for (i = 0; i < R; i++) {
            res *= (N - i);
            res /= (i + 1);
        }
        return res;
    }
 
    // Function to get the count of
    // permutations of the array
    // that satisfy the condition
    function cntPermutation(N) {
        // Stores count of permutations
        // of the array that satisfy
        // the given condition
        var cntPerm;
 
        // Stores the value of C(2N, N)
        var C_2N_N = binCoff(2 * N, N);
 
        // Stores the value of
        // catalan number
        cntPerm = C_2N_N / (N + 1);
 
        // Return answer
        return cntPerm;
    }
 
    // Driver Code
     
        var arr = [ 1, 2, 3, 4 ];
        var N = arr.length;
        document.write(cntPermutation(N / 2));
 
// This code contributed by umadevi9616
</script>
Output
2

Time Complexity: O(N)
Auxiliary Space: O(1)

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :