5 kyu

Simple Fun #273: Powerset

110 of 305myjinxin2015

Description:

Task

For the given set S its powerset is the set of all possible subsets of S.

Given an array of integers nums, your task is to return the powerset of its elements.

Implement an algorithm that does it in a depth-first search fashion. That is, for every integer in the set, we can either choose to take or not take it. At first, we choose NOT to take it, then we choose to take it(see more details in exampele).

Example

For nums = [1, 2], the output should be [[], [2], [1], [1, 2]].

Here's how the answer is obtained:

don't take element 1
----don't take element 2
--------add []
----take element 2
--------add [2]
take element 1
----don't take element 2
--------add [1]
----take element 2
--------add [1, 2]

For nums = [1, 2, 3], the output should be

[[], [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3]].

Input/Output

[input] integer array nums

Array of positive integers, 1 ≤ nums.length ≤ 10.

[output] 2D integer array

The powerset of nums.

Algorithms

Stats:

CreatedMay 12, 2017
PublishedMay 12, 2017
Warriors Trained969
Total Skips38
Total Code Submissions1023
Total Times Completed305
JavaScript Completions110
Ruby Completions28
Python Completions196
Crystal Completions9
Total Stars28
% of votes with a positive feedback rating93% of 77
Total "Very Satisfied" Votes69
Total "Somewhat Satisfied" Votes5
Total "Not Satisfied" Votes3
Total Rank Assessments5
Average Assessed Rank
5 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • GiacomoSorbi Avatar
  • Voile Avatar
  • Awesome A.D. Avatar
  • user9644768 Avatar
  • stellartux Avatar
Ad