Given an array of n distinct elements, count total number of subsets.

Examples:

Input : {1, 2, 3} Output : 8Explanationthe 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 **2 ^{n}**

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

**2**

^{n}Alternate explanation is :

Number of subsets of size 0 = ^{n}C_{0}

Number of subsets of size 1 = ^{n}C_{1}

Number of subsets of size 2 = ^{n}C_{2}

………………..

Total number of subsets = ^{n}C_{0} + ^{n}C_{1} + ^{n}C_{2} + …. + ^{n}C_{n} = **2 ^{n}**

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

## 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. ` `?> ` |

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Minimum number of subsets with distinct elements
- Count subsets having distinct even numbers
- Count number of subsets of a set with GCD equal to a given number
- Number of subsets with sum divisible by M | Set 2
- Finding all subsets of a given set in Java
- Print all subsets of given size of a set
- Sum of all subsets of a set formed by first n natural numbers
- Sum of bitwise OR of all possible subsets of given set
- Sum of bitwise AND of all possible subsets of given set
- Sum of sum of all subsets of a set formed by first N natural numbers
- Maximize the difference between two subsets of a set with negatives
- Count of Subsets of a given Set with element X present in it
- Product of all Subsets of a set formed by first N natural numbers
- Partition of a set into K subsets with equal sum using BitMask and DP
- Partition a set into two subsets such that difference between max of one and min of other is minimized

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.