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

📅  最后修改于: 2021-06-25 17:17:50             🧑  作者: 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个容器(数组的N个索引)中,以使每个容器中至少有一个对象。我们开始将对象放在一行上,而不是开始将对象放到垃圾箱中,第一个垃圾箱的对象将从左侧开始,然后是第二个垃圾箱的对象,依此类推。因此,一旦知道第一个对象进入第二个容器,第一个对象进入第三个容器,等等,就将确定配置。我们可以通过在两个对象之间的某些位置放置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