📜  从N个自然数计算大小为N的非递减子数组

📅  最后修改于: 2021-04-29 14:51:38             🧑  作者: Mango

给定N个自然数,任务是找到可以使用1到N的元素形成的大小为N的子数组的计数,以使子数组中的每个元素小于或等于其右边的元素(a [ i]≤a [i + 1])。

例子:

方法:

  • 由于数组的每个元素都在1到N之间,并且子数组可以具有不降序的重复元素,即a [0]≤a [1]≤…。 ≤a [N – 1]
  • 从n个对象中选择r个对象进行替换的方法数量为{{n+r-1}\choose{r}} (结合重复使用)。
  • 在这里r = N和n = N ,我们可以从1到N中选择。因此,长度为N且元素从1到N的所有排序数组的计数为{{2*n-1}\choose{n}}
  • 现在可以在二项式系数的帮助下进一步扩展。从中获得的系数将是所需子数组的计数。

下面是上述方法的实现:

C++
// C++ program to count non decreasing subarrays
// of size N from N Natural numbers
  
#include 
using namespace std;
  
// Returns value of Binomial Coefficient C(n, k)
int binomialCoeff(int n, int k)
{
    int C[k + 1];
    memset(C, 0, sizeof(C));
  
    // Since nC0 is 1
    C[0] = 1;
  
    for (int i = 1; i <= n; i++) {
  
        // Compute next row of pascal triangle using
        // the previous row
        for (int j = min(i, k); j > 0; j--)
            C[j] = C[j] + C[j - 1];
    }
    return C[k];
}
  
// Function to find the count of required subarrays
int count_of_subarrays(int N)
{
  
    // The required count is the binomial coefficient
    // as explained in the approach above
    int count = binomialCoeff(2 * N - 1, N);
  
    return count;
}
  
// Driver Function
int main()
{
  
    int N = 3;
  
    cout << count_of_subarrays(N) << "\n";
}


Java
// Java program to count non decreasing subarrays
// of size N from N Natural numbers
class GFG
{
  
// Returns value of Binomial Coefficient C(n, k)
static int binomialCoeff(int n, int k)
{
    int []C = new int[k + 1];
  
    // Since nC0 is 1
    C[0] = 1;
  
    for (int i = 1; i <= n; i++)
    {
  
        // Compute next row of pascal triangle using
        // the previous row
        for (int j = Math.min(i, k); j > 0; j--)
            C[j] = C[j] + C[j - 1];
    }
    return C[k];
}
  
// Function to find the count of required subarrays
static int count_of_subarrays(int N)
{
  
    // The required count is the binomial coefficient
    // as explained in the approach above
    int count = binomialCoeff(2 * N - 1, N);
  
    return count;
}
  
// Driver Function
public static void main(String[] args)
{
    int N = 3;
    System.out.print(count_of_subarrays(N)+ "\n");
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 program to count non decreasing subarrays 
# of size N from N Natural numbers 
  
# Returns value of Binomial Coefficient C(n, k) 
def binomialCoeff(n, k) : 
  
    C = [0] * (k + 1);
  
    # Since nC0 is 1 
    C[0] = 1; 
  
    for i in range(1, n + 1) :
  
        # Compute next row of pascal triangle using 
        # the previous row 
        for j in range(min(i, k), 0, -1) : 
            C[j] = C[j] + C[j - 1]; 
      
    return C[k]; 
  
# Function to find the count of required subarrays 
def count_of_subarrays(N) : 
  
    # The required count is the binomial coefficient 
    # as explained in the approach above 
    count = binomialCoeff(2 * N - 1, N); 
  
    return count; 
  
# Driver Function 
if __name__ == "__main__" : 
      
    N = 3; 
  
    print(count_of_subarrays(N)) ; 
  
# This code is contributed by AnkitRai01


C#
// C# program to count non decreasing subarrays
// of size N from N Natural numbers
using System;
  
class GFG
{
  
    // Returns value of Binomial Coefficient C(n, k)
    static int binomialCoeff(int n, int k)
    {
        int []C = new int[k + 1];
      
        // Since nC0 is 1
        C[0] = 1;
      
        for (int i = 1; i <= n; i++)
        {
      
            // Compute next row of pascal triangle using
            // the previous row
            for (int j = Math.Min(i, k); j > 0; j--)
                C[j] = C[j] + C[j - 1];
        }
        return C[k];
    }
      
    // Function to find the count of required subarrays
    static int count_of_subarrays(int N)
    {
      
        // The required count is the binomial coefficient
        // as explained in the approach above
        int count = binomialCoeff(2 * N - 1, N);
      
        return count;
    }
      
    // Driver Function
    public static void Main()
    {
        int N = 3;
        Console.WriteLine(count_of_subarrays(N));
    }
}
  
// This code is contributed by AnkitRai01


输出:
10