Subsets of an Array

Advanced

Let's explore the magical world of arrays! Imagine you have a collection of colorful stones (an array), and you want to see all the possible ways you can choose a bunch of them. This includes choosing none at all, just one stone, or any combination of them until you've chosen all available stones!

In this challenge, your task is to write a function that will help us find all the possible subsets of an array of unique elements. A subset is just a selection of elements from the array, and the order does not matter.

For example:

  • If you have an array [1, 2, 3], the possible subsets are:
    • [] (the empty subset)
    • [1]
    • [2]
    • [3]
    • [1, 2]
    • [1, 3]
    • [2, 3]
    • [1, 2, 3]

Your challenge is to implement the function subsets_of_array(arr: List[int]) -> List[List[int]] that takes a list of integers and returns a list of lists, where the inner lists are all the possible subsets of the input array.

Examples:

subsets_of_array([1, 2]) # returns [ [], [1], [2], [1, 2] ] subsets_of_array([0]) # returns [ [], [0] ] subsets_of_array([]) # returns [ [] ]

List the subsets from smallest to largest, keeping the numbers in each subset in the same order as in the array, just like the examples above. Happy coding!

Geek
Inputs

Arg 1:

[1,2]
Expected Output
[[], [1], [2], [1, 2]]
Actual Output
Subsets of an Array [advanced] | Coding for Kids