Related Articles

Number of distinct subsets of a set

• Difficulty Level : Basic
• Last Updated : 07 Apr, 2021

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 ``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

 ``

Javascript

 ``
Output:
`8`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up