Coders Packet

Print all Subsequences of an Array Using C++

By MOHAMMAD JABIR

This Project Prints all the Subsequences of the Array.Input will be given by User i.e.., an Array and Size of Array 'N' using C++ programming language.

Given an integer array of unique elements By User. 

Have to print all the Subsequences of the array.

The Subsequences must not contain duplicate

Example 1:

Input: a = [1,2,3],N=3
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

Example 2:

Input: a= [0],N=1 
Output: [[],[0]]
#include 
#include<bits/stdc++.h> 
using namespace std;
class Solution {
public:
    void fun(int ind,vector<vector>&result,vector&ans,int n,vector&a) 
    {
        if(ind==n) 
        {
            result.push_back(ans); 
            return ; 
        }
        ans.push_back(a[ind]); 
        fun(ind+1,result,ans,n,a); 
        ans.pop_back(); 
        fun(ind+1,result,ans,n,a); 

    }
    vector<vector> SubSequences(vector&a) {
        vector<vector>result; 
        int n=a.size();
        vectorans; 
        fun(0,result,ans,n,a); 
        sort(result.begin(),result.end()); 
        return result; 
    }
};
int main()
{
    // Let us take vector a sample input  
    vectora; 
    int n=3; 
    a.push_back(1); 
    a.push_back(2);
    a.push_back(3);
    Solution obj; // creation of obj  
    vector<vector>ans=obj.SubSequences(a); 
    for(int i=0;i<ans.size();i++) 
    {
        for(int j=0;j<ans[i].size();j++)
        {
            cout<<ans[i][j]<<" "; 
        }
        cout<<endl; 
    }
    return 0;
}

 

Constraints:

  • 1 <=size of array<= 10
  • all the elements in array are unique given by the User.

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by MOHAMMAD JABIR (mdjabir86)

Download packets of source code on Coders Packet