Generating all possible Subsequences using Recursion including the empty one.
Last Updated :
25 May, 2022
Given an array. The task is to generate and print all of the possible subsequences of the given array using recursion.
Examples:
Input : [1, 2, 3]
Output : [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3], []
Input : [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.
Below diagram shows the recursion tree for array, arr[] = {1, 2}.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void printSubsequences( int arr[], int index,
vector< int > &subarr, int n)
{
if (index == n)
{
for ( auto it:subarr){
cout << it << " " ;
}
if (subarr.size()==0)
cout<< "{}" ;
cout<<endl;
return ;
}
else
{
subarr.push_back(arr[index]);
printSubsequences(arr, index + 1, subarr,n);
subarr.pop_back();
printSubsequences(arr, index + 1, subarr,n);
}
}
int main()
{
int arr[]={1, 2, 3};
int n= sizeof (arr)/ sizeof (arr[0]);
vector< int > vec;
printSubsequences(arr, 0, vec,n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
public static void printSubsequences( int [] arr, int index,
ArrayList<Integer> path)
{
if (index == arr.length)
{
if (path.size() > 0 )
System.out.println(path);
}
else
{
printSubsequences(arr, index + 1 , path);
path.add(arr[index]);
printSubsequences(arr, index + 1 , path);
path.remove(path.size() - 1 );
}
return ;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 };
ArrayList<Integer> path = new ArrayList<>();
printSubsequences(arr, 0 , path);
}
}
|
Python3
def printSubsequences(arr, index, subarr):
if index = = len (arr):
if len (subarr) ! = 0 :
print (subarr)
else :
printSubsequences(arr, index + 1 , subarr)
printSubsequences(arr, index + 1 ,
subarr + [arr[index]])
return
arr = [ 1 , 2 , 3 ]
printSubsequences(arr, 0 , [])
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void printSubsequences( int [] arr, int index, List< int > path)
{
if (index == arr.Length)
{
if (path.Count > 0)
{
Console.Write( "[" );
for ( int i = 0; i < path.Count - 1; i++)
{
Console.Write(path[i] + ", " );
}
Console.WriteLine(path[path.Count - 1] + "]" );
}
}
else
{
printSubsequences(arr, index + 1, path);
path.Add(arr[index]);
printSubsequences(arr, index + 1, path);
path.RemoveAt(path.Count - 1);
}
return ;
}
static void Main() {
int [] arr = { 1, 2, 3 };
List< int > path = new List< int >();
printSubsequences(arr, 0, path);
}
}
|
Javascript
<script>
function printSubsequences(arr, index, path)
{
if (index == arr.length)
{
if (path.length > 0) document.write(`[${path}]<br>`);
}
else
{
printSubsequences(arr, index + 1, path);
path.push(arr[index]);
printSubsequences(arr, index + 1, path);
path.pop();
}
return ;
}
let arr = [1, 2, 3];
let path = new Array();
printSubsequences(arr, 0, path);
</script>
|
output
1 2 3
1 2
1 3
1
2 3
2
3
{}
Time Complexity:
Space Complexity:
O(n) , Because of the recursion stack.
Share your thoughts in the comments
Please Login to comment...