Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Generate All Subsequence

Medium Acceptance 56% Points 30

You are given an array of integers. Your task is to generate all possible subsequences of the array

A subsequence is formed by selecting elements in the same order as the original array, but you may skip any element. The empty subsequence is also allowed.

Examples
Example 1

Example 1

Input: arr[] = [5]

Output: [] [5]

Explanation: The element can be skipped or taken.

Example 2

Example 2

Input: arr[] = [1 2 3]

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

Explanation: Elements are chosen while keeping their original order, forming different subsequences.

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: By including or skipping each element in order, all possible subsequences are generated.

Constraints
  • 1 = n = 20
  • -10? = arr[i] = 10?
Topics
Recursion
Companies
Bloomberg
Added 19 Jul 2026 56% acceptance
Ln 1, Col 1Sign in to save your code
1 2 3
 
1 2 3 12 13 23 123