Number of arrays of size N whose elements are positive integers and sum is K
Last Updated :
07 Jan, 2024
Given two positive integers N and K. The task is to find the number of arrays of size N that can be formed such that elements of the array should be positive integers and the sum of elements is equal to K.
Examples:
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
Prerequisite: Stars and Bars
Suppose there are K identical objects which needs to be placed in N bins (N indices of the array) such that each bin have at least one object. Instead of starting to place objects into bins, we start placing the objects on a line, where the object for the first bin will be taken from the left, followed by the objects for the second bin, and so forth. Thus, the configuration will be determined once one knows what is the first object going to the second bin, and the first object going to the third bin, and so on. We can indicate this by placing N X 1 separating bars at some places between two objects; since no bin is allowed to be empty, there can be at most one bar between a given pair of objects. So, we have K objects in a line with K - 1 gaps. Now we have to choose N - 1 gaps to place bars from K - 1 gaps. This can be chosen by K - 1CN - 1.
Below is implementation of this approach:
C++
// CPP Program to find the number of arrays of
// size N whose elements are positive integers
// and sum is K
#include <bits/stdc++.h>
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
<?php
// PHP Program to find the
// number of arrays of size
// N whose elements are
// positive integers and
// sum is K
// Return nCr
function binomialCoeff($n, $k)
{
$C = array_fill(0, ($k + 1), 0);
$C[0] = 1; // nC0 is 1
for ($i = 1; $i <= $n; $i++)
{
// Compute next row
// of pascal triangle
// using the previous row
for ($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.
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
<script>
// Javascript Program to find the number of arrays of
// size N whose elements are positive integers
// and sum is K
// Return nCr
function binomialCoeff(n, k)
{
var C = Array(k+1).fill(0);
C[0] = 1; // nC0 is 1
for (var i = 1; i <= n; i++) {
// Compute next row of pascal triangle using
// the previous row
for (var 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.
function countArray(N, K)
{
return binomialCoeff(K - 1, N - 1);
}
// Driver Code
var N = 2, K = 3;
document.write( countArray(N, K));
</script>
Time Complexity : O(N*K).
Auxiliary Space: O(K)
Similar Reads
Min sum of set of positive integers so that sum of no two elements is K Given two positive integers N and K. Create a set of size N containing distinct positive integers such that the sum of no two integers is K, the task is to find the minimum possible sum of the set that meets the above criteria. Since the answer may be large, print it modulo 10^9+7. Note: It is guara
6 min read
Count of integers in an Array whose length is a multiple of K Given an array arr of N elements and an integer K, the task is to count all the elements whose length is a multiple of K.Examples: Input: arr[]={1, 12, 3444, 544, 9}, K = 2 Output: 2 Explanation: There are 2 numbers whose digit count is multiple of 2 {12, 3444}. Input: arr[]={12, 345, 2, 68, 7896},
5 min read
Sum of elements of all partitions of number such that no element is less than K Given an integer N, the task is to find an aggregate sum of all integer partitions of this number such that each partition does not contain any integer less than K. Examples: Input: N = 6 and K = 2 Output: 24 In this case, there are 4 valid partitions. 1) {6} 2) {4, 2} 3) {3, 3} 4) {2, 2, 2} Therefo
9 min read
Find K positive integers not exceeding N and having sum S Given three positive integers S, K, and N, the task is to find K distinct positive integers, not exceeding N and having sum equal to S. If it is not possible to find K such positive integers, print -1. Examples: Input: S = 15, K = 4, N = 8Output: 1 2 4 8Explanation:One possible set of K such numbers
9 min read
Find the number of elements X such that X + K also exists in the array Given an array a[] and an integer k, find the number of elements x in this array such that the sum of x and k is also present in the array. Examples: Input: { 3, 6, 2, 8, 7, 6, 5, 9 } and k = 2Output: 5 Explanation:Elements {3, 6, 7, 6, 5} in this array have x + 2 value that is{5, 8, 9, 8, 7} presen
10 min read