VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-possible-permutations-of-given-array-satisfying-the-given-conditions/

⇱ Count possible permutations of given array satisfying the given conditions - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count possible permutations of given array satisfying the given conditions

Last Updated : 23 Jul, 2025

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: 


Output
2

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

Comment