Hone logo
Hone
Problems

Generating All Subsets of a Set

This challenge involves understanding and implementing a fundamental combinatorial problem: finding all possible subsets (also known as the power set) of a given set of distinct elements. This skill is crucial in various algorithmic contexts, including backtracking, dynamic programming, and graph theory problems.

Problem Description

Your task is to write a function that takes a list of distinct integers as input and returns a list of all possible subsets of that input list. A subset is a collection of elements from the original list, where the order of elements within a subset does not matter, and elements can be included or excluded. The output should contain all possible combinations of elements, including the empty set and the set itself.

Key Requirements:

  • The output should be a list of lists, where each inner list represents a unique subset.
  • The order of subsets in the output list does not matter.
  • The order of elements within each subset does not matter.
  • The input list contains distinct integers.

Expected Behavior: For an input list [1, 2, 3], the function should return all possible subsets.

Edge Cases:

  • An empty input list should result in a list containing only the empty set.

Examples

Example 1:

Input: [1, 2, 3]
Output: [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]
Explanation: This output includes all combinations of elements from the input list, from no elements (empty set) to all elements.

Example 2:

Input: [0]
Output: [[], [0]]
Explanation: The subsets are the empty set and the set containing the single element.

Example 3:

Input: []
Output: [[]]
Explanation: The only subset of an empty set is the empty set itself.

Constraints

  • The input list will contain between 0 and 10 distinct integers.
  • Each integer in the input list will be between -100 and 100, inclusive.
  • The total number of subsets for a set of size N is 2^N. The solution should be able to handle this growth.

Notes

Consider approaches that systematically build subsets. Recursion or iterative methods can be employed. Think about how you can decide for each element whether to include it in a subset or not. You might find it helpful to think about this problem as a decision tree.

Loading editor...
plaintext