Total number of Subsets of size at most K
Last Updated :
21 Jan, 2022
Given a number N which is the size of the set and a number K, the task is to find the count of subsets, of the set of N elements, having at most K elements in it, i.e. the size of subset is less than or equal to K.
Examples:
Input: N = 3, K = 2
Output: 6
Subsets with 1 element in it = {1}, {2}, {3}
Subsets with 2 elements in it = {1, 2}, {1, 3}, {1, 2}
Since K = 2, therefore only the above subsets will be considered for length atmost K. Therefore the count is 6.
Input: N = 5, K = 2
Output: 15
Approach:
- Since the number of subsets of exactly K elements that can be made from N items is (NCK). Therefore for "at most", the required count will be
[latex]\LARGE \sum _{i = 1}^{K} \text{ } ^{N}\textrm{C}_{i} =\text{ } ^{N}\textrm{C}_{1} + ^{N}\textrm{C}_{2} + ^{N}\textrm{C}_{3} + ...+ ^{N}\textrm{C}_{K}[/latex]
- Inorder to calculate the value of NCK, Binomial Coefficient is used. Please refer this article to see how it works.
- So to get the required subsets for length atmost K, run a loop from 1 to K and add the NCi for each value of i.
Below is the implementation of the above approach:
C++
// C++ code to find total number of
// Subsets of size at most K
#include <bits/stdc++.h>
using namespace std;
// Function to compute the value
// of Binomial Coefficient C(n, k)
int binomialCoeff(int n, int k)
{
int C[n + 1][k + 1];
int i, j;
// Calculate value of Binomial Coefficient
// in bottom up manner
for (i = 0; i <= n; i++) {
for (j = 0; j <= min(i, k); j++) {
// Base Cases
if (j == 0 || j == i)
C[i][j] = 1;
// Calculate value using previously
// stored values
else
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
}
return C[n][k];
}
// Function to calculate sum of
// nCj from j = 1 to k
int count(int n, int k)
{
int sum = 0;
for (int j = 1; j <= k; j++) {
// Calling the nCr function
// for each value of j
sum = sum + binomialCoeff(n, j);
}
return sum;
}
// Driver code
int main()
{
int n = 3, k = 2;
cout << count(n, k) << endl;
n = 5, k = 2;
cout << count(n, k) << endl;
return 0;
}
Java
// Java code to find total number of
// Subsets of size at most K
import java.lang.*;
class GFG
{
// Function to compute the value
// of Binomial Coefficient C(n, k)
public static int binomialCoeff(int n, int k)
{
int[][] C = new int[n + 1][k + 1];
int i, j;
// Calculate value of Binomial Coefficient
// in bottom up manner
for (i = 0; i <= n; i++)
{
for (j = 0; j <= Math.min(i, k); j++)
{
// Base Cases
if (j == 0 || j == i)
C[i][j] = 1;
// Calculate value using previously
// stored values
else
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
}
return C[n][k];
}
// Function to calculate sum of
// nCj from j = 1 to k
public static int count(int n, int k)
{
int sum = 0;
for (int j = 1; j <= k; j++)
{
// Calling the nCr function
// for each value of j
sum = sum + binomialCoeff(n, j);
}
return sum;
}
// Driver code
public static void main(String args[])
{
GFG g = new GFG();
int n = 3, k = 2;
System.out.print(count(n, k));
int n1 = 5, k1 = 2;
System.out.print(count(n1, k1));
}
}
// This code is contributed by SoumikMondal
Python3
# Python code to find total number of
# Subsets of size at most K
# Function to compute the value
# of Binomial Coefficient C(n, k)
def binomialCoeff(n, k):
C = [[0 for i in range(k + 1)] for j in range(n + 1)];
i, j = 0, 0;
# Calculate value of Binomial Coefficient
# in bottom up manner
for i in range(n + 1):
for j in range( min(i, k) + 1):
# Base Cases
if (j == 0 or j == i):
C[i][j] = 1;
# Calculate value using previously
# stored values
else:
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
return C[n][k];
# Function to calculate sum of
# nCj from j = 1 to k
def count(n, k):
sum = 0;
for j in range(1, k+1):
# Calling the nCr function
# for each value of j
sum = sum + binomialCoeff(n, j);
return sum;
# Driver code
if __name__ == '__main__':
n = 3;
k = 2;
print(count(n, k), end="");
n1 = 5;
k1 = 2;
print(count(n1, k1));
# This code is contributed by 29AjayKumar
C#
// C# code to find total number of
// Subsets of size at most K
using System;
class GFG
{
// Function to compute the value
// of Binomial Coefficient C(n, k)
public static int binomialCoeff(int n, int k)
{
int[,] C = new int[n + 1, k + 1];
int i, j;
// Calculate value of Binomial Coefficient
// in bottom up manner
for (i = 0; i <= n; i++)
{
for (j = 0; j <= Math.Min(i, k); j++)
{
// Base Cases
if (j == 0 || j == i)
C[i, j] = 1;
// Calculate value using previously
// stored values
else
C[i, j] = C[i - 1, j - 1] + C[i - 1, j];
}
}
return C[n, k];
}
// Function to calculate sum of
// nCj from j = 1 to k
public static int count(int n, int k)
{
int sum = 0;
for (int j = 1; j <= k; j++)
{
// Calling the nCr function
// for each value of j
sum = sum + binomialCoeff(n, j);
}
return sum;
}
// Driver code
public static void Main()
{
int n = 3, k = 2;
Console.Write(count(n, k));
int n1 = 5, k1 = 2;
Console.Write(count(n1, k1));
}
}
// This code is contributed by AnkitRai01
JavaScript
<script>
// Javascript implementation of the
// above approach
// Function for the binomial coefficient
function binomialCoeff(n, k)
{
var C = new Array(n + 1);
// Loop to create 2D array using 1D array
for (var i = 0; i < C.length; i++) {
C[i] = new Array(k + 1);
}
var i, j;
// Calculate value of Binomial Coefficient
// in bottom up manner
for (i = 0; i <= n; i++) {
for (j = 0; j <= Math.min(i, k); j++) {
// Base Cases
if (j == 0 || j == i)
C[i][j] = 1;
// Calculate value using previously
// stored values
else
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
}
return C[n][k];
}
// Function to calculate sum of
// nCj from j = 1 to k
function count(n, k)
{
var sum = 0;
for (var j = 1; j <= k; j++) {
// Calling the nCr function
// for each value of j
sum = sum + binomialCoeff(n, j);
}
return sum;
}
// Driver code
var n = 3;
var k = 2;
document.write(count(n, k));
var n = 5;
var k = 2;
document.write(count(n, k));
// This code is contributed by ShubhamSingh10
</script>
Time Complexity: O(n2 * k)
Auxiliary Space: O(n + k)
Similar Reads
Number of distinct subsets of a set Given an array of n distinct elements, count total number of subsets. Examples: Input : {1, 2, 3} Output : 8 Explanation: the array contain total 3 element.its subset are {}, {1}, {2}, {3}, {1, 2}, {2, 3}, {3, 1}, {1, 2, 3}. so the output is 8.. We know number of subsets of set of size n is 2n How d
3 min read
Count number of ways to partition a set into k subsets Given two numbers n and k where n represents a number of elements in a set, find a number of ways to partition the set into k subsets.Example: Input: n = 3, k = 2Output: 3Explanation: Let the set be [1, 2, 3], we can partition it into 3 subsets in the following ways [[1,2], [3]], [[1], [2,3]], [[1,3
15+ min read
Sum of all subsets of a given size (=K) Given an array arr[] consisting of N integers and a positive integer K, the task is to find the sum of all the subsets of size K. Examples: Input: arr[] = {1, 2, 4, 5}, K = 2Output: 36Explanation:The subsets of size K(= 2) are = {1, 2}, {1, 4}, {1, 5}, {2, 4}, {2, 5}, {4, 5}. Now, the sum of all sub
7 min read
Count of all possible combinations of K numbers that sums to N Given a number N, the task is to count the combinations of K numbers from 1 to N having a sum equal to N, with duplicates allowed. Example: Input: N = 7, K = 3Output:15Explanation:The combinations which lead to the sum N = 7 are: {1, 1, 5}, {1, 5, 1}, {5, 1, 1}, {2, 1, 4}, {1, 2, 4}, {1, 4, 2}, {2,
11 min read
Print all subsets of given size of a set Generate all possible subsets of size r of the given array with distinct elements. Examples: Input : arr[] = {1, 2, 3, 4} r = 2Output : 1 2 1 3 1 4 2 3 2 4 3 4Input : arr[] = {10, 20, 30, 40, 50} r = 3Output : 10 20 30 10 20 40 10 20 50 10 30 40 10 30 50 10 40 50 20 30 40 20 30 50 20 40 50 30 40 50
15+ min read