Video summary
L6. Recursion on Subsequences | Printing Subsequences
Main summary
Key takeaways
Main ideas & lessons
- Goal of the video: Teach how to print all subsequences of an array using recursion.
- Key definition (subsequence vs subarray):
- A subsequence is any selection of elements that maintains the original order of the array.
- Elements in a subsequence can be contiguous or non-contiguous.
- A subarray is always contiguous, and it can be seen as a special case of subsequence.
- Example array:
[3, 1, 2](sizen = 3)- Single elements like
3,1,2are subsequences. - Multi-element subsequences include:
3 1,3 2,1 2,3 1 2. - The empty subsequence is also included (select nothing).
- Total number of subsequences for
n = 3is2^3 = 8.
- Single elements like
Core recursion concept: “take” vs “not take”
For each index, there are two choices:
- Take the current element into the running subsequence.
- Not take the current element (skip it).
By making these decisions independently for every index, you generate all subsequences.
Take/Not-take mapping (pattern intuition)
Subsequence generation follows a binary-choice pattern:
- Example subsequence
3, 2corresponds to:- take index of
3 - not take index of
1 - take index of
2
- take index of
Recursive methodology (step-by-step)
Inputs to the recursion
index: current position in the arrayds: a dynamic list/vector holding the subsequence built so far
Base case
- If
index >= n:- Print
ds(including the emptyds, which represents the empty subsequence) - Return
- Print
Recursive step (two branches)
For the current index, create two branches:
-
Branch 1: Take current element
- Add
arr[index]tods - Recurse with
index + 1using the updatedds - After returning, remove the last added element from
ds(backtracking / “pop back”)
- Add
-
Branch 2: Not take current element
- Do not modify
ds - Recurse with
index + 1
- Do not modify
Backtracking necessity
After exploring the “take” branch, you must undo that choice before exploring “not take” at the same recursion level; otherwise, subsequences would incorrectly accumulate extra elements.
Recursion tree explanation (conceptual)
- The recursion forms a tree where:
- Each level corresponds to an array index.
- Each node splits into take and not take.
- When
index == n, the builtdsis printed. - Example output includes:
3 1 23 13 231 212- empty subsequence
Complexity analysis
- Time complexity:
O(2^n * n)- There are
2^nsubsequences. - Printing each subsequence can cost up to
O(n)overall.
- There are
- Space complexity (auxiliary stack):
O(n)- Recursion depth is at most
n.
- Recursion depth is at most
Speakers / sources featured
- Speaker: Video host/instructor (name not provided in subtitles; e.g., “hey everyone welcome back…”)
- Sources: None external cited. (Only the general “power set” idea is referenced.)
Rate this summary
Your feedback will help improve summaries.
Improve this summary
Reprocess with a stronger model when the summary feels incomplete or inaccurate.
Translate summary in another language
Ask questions to this video
Chat for follow-up questions, clarifications, and source-backed answers.