📌  相关文章
📜  元素为正整数且总和为 K 的大小为 N 的数组的数量

📅  最后修改于: 2021-09-17 07:44:33             🧑  作者: Mango

给定两个正整数NK 。任务是找到可以形成的大小为 N 的数组的数量,使得数组的元素应该是正整数,并且元素的总和等于 K。
例子:

Input : N = 2, K = 3
Output : 2
Explanation: [1, 2] and [2, 1] are the only arrays of size 2 whose sum is 3.

Input : n = 3, k = 7
Output : 15

先决条件:星条旗

假设有 K 个相同的对象需要放置在 N 个 bin(数组的 N 个索引)中,这样每个 bin 至少有一个对象。我们不是开始将对象放入 bin 中,而是开始将对象放在一条线上,第一个 bin 的对象将从左侧取出,然后是第二个 bin 的对象,依此类推。因此,一旦知道什么是第一个物体进入第二个箱子,第一个物体进入第三个箱子,就会确定配置,依此类推。我们可以通过在两个对象之间的某些位置放置 NX 1 分隔条来表明这一点;由于不允许垃圾箱为空,因此给定的一对对象之间最多可以有一个条形。所以,我们有 K 个对象在一条线上,有 K-1 个间隙。现在我们必须选择 N – 1 个间隙来放置 K – 1 个间隙中的钢筋。这可以由K – 1 C N – 1 选择
下面是这种方法的实现:

C++
// CPP Program to find the number of arrays of
// size N whose elements are positive integers
// and sum is K
#include 
using namespace std;
 
// Return nCr
int binomialCoeff(int n, int k)
{
    int C[k + 1];
    memset(C, 0, sizeof(C));
 
    C[0] = 1; // nC0 is 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];
}
 
// Return the number of array that can be
// formed of size n and sum equals to k.
int countArray(int N, int K)
{
    return binomialCoeff(K - 1, N - 1);
}
 
// Driver Code
int main()
{
    int N = 2, K = 3;
 
    cout << countArray(N, K) << endl;
 
    return 0;
}


Java
// Java Program to find the
// number of arrays of size
// N whose elements are positive
// integers and sum is K
import java.io.*;
 
class GFG
{
     
// Return nCr
static int binomialCoeff(int n,
                         int k)
{
    int []C = new int[k + 1];
 
 
    C[0] = 1; // nC0 is 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];
}
 
// Return the number of
// array that can be
// formed of size n and
// sum equals to k.
static int countArray(int N, int K)
{
    return binomialCoeff(K - 1, N - 1);
}
 
// Driver Code
public static void main (String[] args)
{
        int N = 2, K = 3;
 
System.out.println( countArray(N, K));
}
}
 
// This code is contributed by anuj_67.


Python3
# Python3 Program to find the number
# of arrays of size N whose elements
# are positive integers and sum is K
 
# Return nCr
def binomialCoeff(n, k):
 
    C = [0] * (k + 1);
     
    C[0] = 1; # nC0 is 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];
 
# Return the number of array that
# can be formed of size n and
# sum equals to k.
def countArray(N, K):
 
    return binomialCoeff(K - 1, N - 1);
 
# Driver Code
N = 2;
K = 3;
 
print(countArray(N, K));
 
# This code is contributed by mits


C#
// C# Program to find the
// number of arrays of size
// N whose elements are positive
// integers and sum is K
using System;
 
class GFG
{
// Return nCr
static int binomialCoeff(int n,
                         int k)
{
    int []C = new int[k + 1];
 
 
    C[0] = 1; // nC0 is 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];
}
 
// Return the number of
// array that can be
// formed of size n and
// sum equals to k.
static int countArray(int N,
                      int K)
{
    return binomialCoeff(K - 1,
                         N - 1);
}
 
// Driver Code
static public void Main ()
{
    int N = 2, K = 3;
 
    Console.WriteLine(
            countArray(N, K));
}
}
 
// This code is contributed by ajit


PHP
 0; $j--)
            $C[$j] = $C[$j] +
                     $C[$j - 1];
    }
    return $C[$k];
}
 
// Return the number of
// array that can be
// formed of size n and
// sum equals to k.
function countArray($N, $K)
{
    return binomialCoeff($K - 1,
                         $N - 1);
}
 
// Driver Code
$N = 2;
$K = 3;
 
echo countArray($N, $K);
 
// This code is contributed by mits
?>


Javascript


输出:

2

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程