📌  相关文章
📜  计算满足给定条件的给定数组的可能排列

📅  最后修改于: 2021-09-03 03:09:16             🧑  作者: Mango

给定一个数组arr[]N 个不同的元素组成,任务是计算可以生成的满足以下属性的给定数组的可能排列:

  • 两半必须排序。
  • arr[i]必须小于arr[N / 2 + i]

注意: N 总是偶数,索引从0开始。

例子:

方法:按照以下步骤解决问题:

  • 初始化一个变量,比如cntPerm来存储满足给定条件的给定数组的排列计数。
  • 使用以下公式求出2N C N的二项式系数的值:
  • 最后,计算catalan number = 2N C N / (N + 1)并将其打印为所需答案。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
 
#include 
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


输出
2

时间复杂度:O(N)
辅助空间:O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live