Number of sub-sequences in a given sequence
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Numbering and analyzing subsequences in a given sequence is a fundamental concept in the field of combinatorics, a branch of mathematics dealing with combinations of objects. Understanding how many subsequences can be formed from a sequence provides insights not only in pure mathematics but also in computer science, especially in algorithm design and data analysis.
Understanding Subsequences
A subsequence is derived from a sequence by deleting zero or more elements without changing the order of the remaining elements. For example, from the sequence `[1, 2, 3]`, possible subsequences include `[1]`, `[1, 2]`, `[2, 3]`, and `[1, 3]`. The original sequence and an empty sequence are also considered subsequences, making the total list `[1]`, `[2]`, `[3]`, `[1, 2]`, `[2, 3]`, `[1, 3]`, `[1, 2, 3]`, and `[]`.
Basic Formula
For a sequence of length `n`, the number of possible subsequences is determined by the formula:
This formula accounts for the choice of including or excluding each element in the sequence. Therefore, with `n` elements in the sequence, each has two options: to be included or not.
Example: Consider the sequence `[a, b, c]`. The possible subsequences are:
- Choosing no elements (empty subsequence): `[]`
- Choosing one element: `[a]`, `[b]`, `[c]`
- Choosing two elements: `[a, b]`, `[a, c]`, `[b, c]`
- Choosing all elements (the whole sequence): `[a, b, c]`
This results in subsequences.
Special Case: Distinct and Non-Distinct Elements
When dealing with sequences, it’s crucial to recognize whether the elements are distinct. If they're non-distinct (i.e., there are repeated elements), the calculation of distinct subsequences changes.
For instance, for the sequence `[1, 1, 2]`, directly applying would suggest 8 subsequences, but this count would include duplicates. To address the uniqueness condition, advanced combinatorial techniques or dynamic programming methods are required to enumerate only distinct subsequences.
Dynamic Programming Approach
One efficient method to find distinct subsequences is using dynamic programming, particularly useful in constraints with repeated elements. Here's how it works at a high level:
- Initialize a list `dp` where `dp[i]` represents the number of distinct subsequences possible for the sequence up to the i-th element.
- Initialize `dp[0] = 1` as the empty subsequence.
- Iterate through the sequence, calculating the number of distinct subsequences considering the current element and previously found subsequences.
- Use a dictionary to keep track of the last occurrence of elements to avoid counting duplicates.
Key Concepts Summary
| Concept | Description |
| Subsequence Definition | A sequence derived by removing zero or more elements without changing order. |
| Basic Counting Formula | where n is the number of elements in the sequence. |
| Distinct Consideration | Uses combinatorial logic or algorithms to account for non-unique elements. |
| Dynamic Programming | A method to efficiently find distinct subsequences in sequences with repeats. |
Applications and Advanced Topics
Subsequences are valuable in various computational scenarios, including:
- Pattern Recognition and String Matching: Algorithms like Longest Common Subsequence (LCS) explore commonalities between sequences.
- Data Compression: Understanding subsequence structures can lead to optimization in data storage.
- Biological Data: Genetic sequencing involves finding subsequences within DNA strands to find mutations or similarities.
Understanding subsequences goes beyond the mere count; it delves into the investigation of properties and relationships among elements comprising the sequence. This calls for more advanced mathematical tools and can significantly elevate computational efficiency and decision-making in many areas of technology and science.
Related reading
- Number of subarrays divisible by k
- Number of substrings in range l, r that can be permuted to palindrome
- Number of ways of correctly arranging parenthesis
- Number of ways to make change for amount N
- Number of ways to divide n objects in k groups, such that no group will have fewer objects than previously formed groups?
- Number of Zeros in the binary representation of an Integer
- Numpy argsort - what is it doing?
- O1 algorithm to determine if node is descendant of another node in a multiway tree?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.