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: 2
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++
#include <bits/stdc++.h>
using namespace std;
int binCoff( int N, int R)
{
int res = 1;
if (R > (N - R)) {
R = (N - R);
}
for ( int i = 0; i < R; i++) {
res *= (N - i);
res /= (i + 1);
}
return res;
}
int cntPermutation( int N)
{
int cntPerm;
int C_2N_N = binCoff(2 * N, N);
cntPerm = C_2N_N / (N + 1);
return cntPerm;
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << cntPermutation(N / 2);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int binCoff( int N,
int R)
{
int res = 1 ;
if (R > (N - R))
{
R = (N - R);
}
for ( int i = 0 ; i < R; i++)
{
res *= (N - i);
res /= (i + 1 );
}
return res;
}
static int cntPermutation( int N)
{
int cntPerm;
int C_2N_N = binCoff( 2 * N, N);
cntPerm = C_2N_N / (N + 1 );
return cntPerm;
}
public static void main (String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 };
int N = arr.length;
System.out.println(cntPermutation(N / 2 ));
}
}
|
Python3
def binCoff(N, R):
res = 1
if (R > (N - R)):
R = (N - R)
for i in range (R):
res * = (N - i)
res / / = (i + 1 )
return res
def cntPermutation(N):
C_2N_N = binCoff( 2 * N, N)
cntPerm = C_2N_N / / (N + 1 )
return cntPerm
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 ]
N = len (arr)
print (cntPermutation(N / / 2 ))
|
C#
using System;
class GFG{
static int binCoff( int N,
int R)
{
int res = 1;
if (R > (N - R))
{
R = (N - R);
}
for ( int i = 0; i < R; i++)
{
res *= (N - i);
res /= (i + 1);
}
return res;
}
static int cntPermutation( int N)
{
int cntPerm;
int C_2N_N = binCoff(2 * N, N);
cntPerm = C_2N_N / (N + 1);
return cntPerm;
}
public static void Main(String[] args)
{
int []arr = {1, 2, 3, 4};
int N = arr.Length;
Console.WriteLine(cntPermutation(N / 2));
}
}
|
Javascript
<script>
function binCoff(N , R) {
var res = 1;
if (R > (N - R)) {
R = (N - R);
}
for (i = 0; i < R; i++) {
res *= (N - i);
res /= (i + 1);
}
return res;
}
function cntPermutation(N) {
var cntPerm;
var C_2N_N = binCoff(2 * N, N);
cntPerm = C_2N_N / (N + 1);
return cntPerm;
}
var arr = [ 1, 2, 3, 4 ];
var N = arr.length;
document.write(cntPermutation(N / 2));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...