Skip to content
Related Articles

Related Articles

Count possible permutations of given array satisfying the given conditions
  • Last Updated : 21 Oct, 2020

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :