Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Generate All Subsets

Medium Acceptance 60.00% Points 30.00

You are given an array of integers. Your task is to generate all possible subsets (power set) of the array. A subset can contain any number of elements, including none or all and you may show none as -.

Examples
Example 1

Example 1

Input: arr[] = [4]

Output: [] [4]

Explanation: The single element is either excluded or included, giving two subsets.

Example 2

Example 2

Input: arr[] = [1 2 3]

Output: [] [1] [2] [3] [1 2] [1 3] [2 3] [1 2 3]

Explanation: Each element has two choices (take or skip), so different combinations form all subsets.

Example 3

Example 3

Input: arr[] = [1 2 3 4]

Output: [] [1] [2] [3] [4] [1 2] [1 3] [1 4] [2 3] [2 4] [3 4] [1 2 3] [1 2 4] [1 3 4] [2 3 4] [1 2 3 4]

Explanation: Since every element can be included or excluded, all possible combinations are generated.

Hints
Hint 1
N/A
Constraints
  • 1 = n = 20
  • -10? = arr[i] = 10?
Companies
Samsung
Topics
Recursion
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions