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: = [{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 using namespace std; // Function to get the value// of binomial coefficientint 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 conditionint 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 Codeint main(){     int arr[] = { 1, 2, 3, 4 };    int N = sizeof(arr) / sizeof(arr);    cout << cntPermutation(N / 2);     return 0;}

Java

 // Java Program to implement// the above approachimport java.io.*;class GFG{  // Function to get the value// of binomial coefficientstatic 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 conditionstatic 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 Codepublic 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 coefficientdef 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 conditiondef 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 Codeif __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 approachusing System;class GFG{  // Function to get the value// of binomial coefficientstatic 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 conditionstatic 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 Codepublic 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


Output
2

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

My Personal Notes arrow_drop_up