Search
 
SCRIPT & CODE EXAMPLE
 

CPP

subsets of a given array

// 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<int> subarr,int n)
{
    // Print the subsequence when reach
    // the leaf of recursion tree
    if (index == n)
    {
         for (auto it:subarr){
           cout << it << " ";
          
         }
      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<int> vec;
      
 
    printSubsequences(arr, 0, vec,n);
 
    return 0;
}
 
// This code is contributed by
// vivekr4400
Comment

PREVIOUS NEXT
Code Example
Cpp :: 5 program code in c++ of friend function 
Cpp :: How to assign two dimensional initializer list in c++ 
Cpp :: 1672. Richest Customer Wealth leetcode solution in c++ 
Cpp :: displaying m images one window opencv c++ 
Cpp :: show mouse c++ 
Cpp :: how to use printf with microseconds c++ 
Cpp :: c++ union set q5 
Cpp :: convert c++ program to c online 
Cpp :: The program must enter a natural number n from the console and find the number previous to n that is not divisible by 2 , 3 and 5 . 
Cpp :: how to input a file path in c++ 
Cpp :: what is a string called in c++ 
Cpp :: onactorbeginoverlap c++ 
Cpp :: void does not a name a type in cpp 
Cpp :: Chef and Feedback codechef solution in cpp 
Cpp :: how to get the numbers in a vector c++ sfml 
Cpp :: what is c++ function 
Cpp :: c++ shift array to the right 
Cpp :: deletion in bst 
Cpp :: what is push() c++ 
Cpp :: passare un array a una funzione 
Cpp :: c++ forloop 
C :: colourful text in c 
C :: how to get time and date in c 
C :: lewis hamilton 
C :: remove on condtion in vec rust 
C :: bash convert find to array 
C :: reattach screen linux 
C :: A binary tree whose every node has either zero or two children is called 
C :: how to print the first character of a string in c 
C :: string input c 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =