samedi 8 août 2020

Is there any way to store sub-sequence of an array in C++?

I want an array that stores all the subsequence of elements present in the array.

Say I have an array Arr=[1,2,3];, is there any way to create different array say SubseqArr=[3,2,23,1,13,12,123]; ?

Ofcourse size of SubseqArr will be 2^(size of Arr) - 1

NOTE: I just don't want to print, I want to store each element 1 by 1 eg; 2 3 but I want to concatenate it to an integer 23 and store it in SubseqArr.

Printing 1 2 3 as last sub-sequence is easy but concatenating it to 123 as integer and storing it in array is tough, I'm not getting how to do it.

The following code didn't help at all:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int unt;

int main(){
    unt t=1;
    //cin>>t;
    while(t--){
        int n;
        cin>>n;
        int arr[n];
        for(int i=0;i<n;i++)
            cin>>arr[i];

        int total=1<<n;
        for(int k=1;k<total;k++){
            for(int i=0;i<n;i++){
                if(k&(1<<i))
                    cout<<arr[i]<<" ";
            }
            cout<<endl;
        }
    }

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire