// C++ code to print all possible // subsequences for given array using // recursion…
- User
// C++ code to print all possible // subsequences for given array using // recursion #include <bits/stdc++.h> using namespace std;
// Recursive function to print all // possible subsequences for given array void printSubsequences(int arr[], int index, vector &subarr,int n) { // Print the subsequence when reach // the leaf of recursion tree if (index == n) { for (auto it:subarr){ cout << it << " ";
code} if(subarr.size()==0) cout<<"{}"; cout<<endl; return; } else { //pick the current index into the subsequence. subarr.push_back(arr[index]); printSubsequences(arr, index + 1, subarr,n); subarr.pop_back(); //not picking the element into the subsequence. printSubsequences(arr, index + 1, subarr,n); }}
// Driver Code int main() { int arr[]={1, 2, 3}; int n=sizeof(arr)/sizeof(arr[0]); vector vec;
codeprintSubsequences(arr, 0, vec,n); return 0;}
// This code is contributed by // vivekr4400 give code in c
- Assistant
Here is a C version of the given C++ code, which prints all possible subsequences of a given array using recursion:
c - User
#include <stdio.h>
// Function to find the subsets of the given array void findSubsets(int nums[], int n) { // Loop through all possible subsets using bit manipulation for (int i = 0; i < (1 << n); i++) {
code// Loop through all elements of the input array for (int j = 0; j < n; j++) { // Check if the jth bit is set in the current subset if ((i & (1 << j)) != 0) { // If the jth bit is set, add the jth element to the subset printf("%d ", nums[j]); } } printf("\n"); }}
// Driver Code int main() { int arr[] = { 1, 2, 3 }; int n = sizeof(arr) / sizeof(arr[0]); findSubsets(arr, n);
codereturn 0;}
modify this code such that it checks if the subset is a subsequence
- Assistant