![]() |
VOOZH | about |
Given an array arr[]. The task is to find all the possible subsequences of the given array using recursion.
Examples:
Input: arr[] = [1, 2, 3]
Output : [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3], []
Input: arr[] = [1, 2]
Output : [2], [1], [1, 2], []
Approach:
For every element in the array, there are two choices, either to include it in the subsequence or not include it. Apply this for every element in the array starting from index 0 until we reach the last index. Print the subsequence once the last index is reached.
The below diagram shows the recursion tree for array, arr[] = [1, 2].
1 2 3 1 2 1 3 1 2 3 2 3
Time complexity: O(2^n)
Auxiliary Space: O(n)
Related article: