Number of distinct subsets of a set
Last Updated :
02 Aug, 2022
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 does this formula work?
For every element, we have two choices, we either pick it or do not pick it. So in total we have 2 * 2 * ... (n times) choices which is 2n
Alternate explanation is :
- Number of subsets of size 0 = nC0
- Number of subsets of size 1 = nC1
- Number of subsets of size 2 = nC2
- ....................
- Total number of subsets = nC0 + nC1 + nC2 + .... + nCn = 2n
Please refer Sum of Binomial Coefficients for details.
Implementation:
C++
// CPP program to count number of distinct
// subsets in an array of distinct numbers
#include <bits/stdc++.h>
using namespace std;
// Returns 2 ^ n
int subsetCount(int arr[], int n)
{
return 1 << n;
}
/* Driver program to test above function */
int main()
{
int A[] = { 1, 2, 3 };
int n = sizeof(A) / sizeof(A[0]);
cout << subsetCount(A, n);
return 0;
}
Java
// Java program to count number of distinct
// subsets in an array of distinct numbers
class GFG {
// Returns 2 ^ n
static int subsetCount(int arr[], int n)
{
return 1 << n;
}
/* Driver program to test above function */
public static void main(String[] args)
{
int A[] = { 1, 2, 3 };
int n = A.length;
System.out.println(subsetCount(A, n));
}
}
// This code is contributed by Prerna Saini.
Python3
# Python3 program to count number
# of distinct subsets in an
# array of distinct numbers
import math
# Returns 2 ^ n
def subsetCount(arr, n):
return 1 << n
# driver code
A = [ 1, 2, 3 ]
n = len(A)
print(subsetCount(A, n))
# This code is contributed by Gitanjali.
C#
// C# program to count number of distinct
// subsets in an array of distinct numbers
using System;
class GFG {
// Returns 2 ^ n
static int subsetCount(int []arr, int n)
{
return 1 << n;
}
// Driver program
public static void Main()
{
int []A = { 1, 2, 3 };
int n = A.Length;
Console.WriteLine(subsetCount(A, n));
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP program to count
// number of distinct
// subsets in an array
// of distinct numbers
// Returns 2 ^ n
function subsetCount($arr, $n)
{
return 1 << $n;
}
// Driver Code
$A = array( 1, 2, 3 );
$n = sizeof($A);
echo(subsetCount($A, $n));
// This code is contributed by Ajit.
?>
JavaScript
<script>
// JavaScript program to count number of distinct
// subsets in an array of distinct numbers
// Returns 2 ^ n
function subsetCount(arr, n)
{
return 1 << n;
}
// Driver code
let A = [ 1, 2, 3 ];
let n = A.length;
document.write(subsetCount(A, n));
</script>
Similar Reads
Total number of Subsets of size at most K 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} S
7 min read
CSES Solutions - Distinct Numbers You are given a list of N integers arr[], and your task is to calculate the number of distinct values in the list. Examples: Input: N = 5, arr[] = {2, 2, 3, 3, 2}Output: 2Explanation: There are only two distinct elements: 2 and 3 in the array arr[]. Input: N = 6, arr[] = {1, 1, 2, 2, 3, 4}Output: 4E
5 min read
Count subsets having distinct even numbers Given a sequence of n numbers. The task is to count all the subsets of the given set which only have even numbers and all are distinct. Note: By the property of sets, if two subsets have the same set of elements then they are considered as one. For example: [2, 4, 8] and [4, 2, 8] are considered to
5 min read
Count number of distinct sum subsets within given range Given a set S of N numbers and a range specified by two numbers L (Lower Bound) and R (Upper Bound). Find the number of distinct values of all possible sums of some subset of S that lie between the given range. Examples : Input : S = { 1, 2, 2, 3, 5 }, L = 1 and R = 5 Output : 5 Explanation : Every
8 min read
Find all distinct subset (or subsequence) sums of an array Given an array arr[] of size n, the task is to find a distinct sum that can be generated from the subsets of the given sets and return them in increasing order. It is given that the sum of array elements is small.Examples: Input: arr[] = [1, 2]Output: [0, 1, 2, 3]Explanation: Four distinct sums can
15+ min read