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 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.
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. ?> |
8
Recommended Posts:
- Minimum number of subsets with distinct elements
- Count subsets having distinct even numbers
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Count number of subsets of a set with GCD equal to a given number
- Number of subsets with zero sum
- Number of subsets with sum divisible by m
- Number of subsets with product less than k
- Total number of subsets in which the product of the elements is even
- Count number of subsets whose median is also present in the same subset
- Count minimum number of subsets (or subsequences) with consecutive numbers
- Print a number strictly less than a given number such that all its digits are distinct.
- Check whether a number has exactly three distinct factors or not
- Number of distinct integers obtained by lcm(X, N)/X
- Queries to find whether a number has exactly four distinct factors or not
- Queries for number of distinct integers in Suffix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : jit_t



