Skip to content
CampusEduX

Generate All Subsets

Mediumjavapythonccppjavascript

Given an array of distinct integers, generate every subset (the power set).

Input Format

The first line contains an integer N. The second line contains N space-separated distinct integers.

Output Format

Print 2^N lines, one per subset, in order of the binary mask 0 .. 2^N - 1. For a mask, list the elements at set bit positions in index order, separated by single spaces. The empty subset is printed as an empty line.

Related Problems

  • Generate All Permutations
    Medium · java, python, c, cpp, javascript
    Solve Problem
  • Tower of Hanoi
    Medium · java, python, c, cpp, javascript
    Solve Problem
  • Rat in a Maze
    Medium · java, python, c, cpp, javascript
    Solve Problem

Input (stdin)

Output

Run your code to see output here...