Skip to content

Commit e5782c0

Browse files
authored
Create Printing Subsequences
1 parent 5d52099 commit e5782c0

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std ;
4+
5+
void printF(int index , vector<int> &ds , int arr[] , int n)
6+
{
7+
if(index == n)
8+
{
9+
// print the vector
10+
for(auto it : ds)
11+
{
12+
cout<< it << " " ;
13+
}
14+
15+
if(ds.size() == 0)
16+
{
17+
cout<< "{}" ;
18+
}
19+
cout<<endl;
20+
return ;
21+
}
22+
23+
// take or pick the particular index into the subsequence
24+
ds.push_back(arr[index]);
25+
printF(index + 1 , ds , arr , n);
26+
ds.pop_back();
27+
28+
29+
// not pick, or not take condition, this elemnt is not added to your subsequence
30+
printF(index + 1 , ds , arr , n);
31+
32+
}
33+
34+
int main() {
35+
36+
int arr[] = {3 , 1 , 2} ;
37+
int n = 3 ; // size of array ;
38+
vector<int> ds ; // to carry all subsequence
39+
printF(0 , ds , arr , n);
40+
41+
return 0;
42+
}
43+
44+
45+
46+
//tc 2^n
47+
//sc n
48+
//https://www.youtube.com/watch?v=AxNNVECce8c&list=PLgUwDviBIf0rGlzIn_7rsaR2FQ5e6ZOL9&index=6

0 commit comments

Comments
 (0)