DevPath

← All problems

Count Subsets

Easy backtracking · math

Description

Return the number of subsets of a set of n distinct elements.

Constraints

0 <= n <= 30

Examples

Input: n = 3

Output: 8

Hints (1)
  • Each element is either in or out, giving 2^n subsets.

Solutions

Powers of two. · Time O(1) · Space O(1)

func countSubsets(n int) int {
    return 1 << n
}

Create a free account to submit solutions, track progress and save problems.

Create account