Question

Q2. Answer the following questions assignment, you will develop a well-documented pseudocode that generates all possible subs

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Approach Used:

Iterate from 0 to the size of the power set.

For each index check at what all positions the bit 1 is set.

Take the values from the Input set at those positions in the index where the 1 is set and insert into the stack.

At the end insert the set into the queue.

#include <iostream>
#include <stack>
#include <queue>
#include <vector>

using namespace std;

int main()
{
   vector<int> inputSet({ 2,4,7,9 });
   queue<stack<int>> myPowerSet;

   int nlenght = inputSet.size();

   for (size_t i = 0; i < pow(nlenght,2); i++)
   {
       int cnt = 0;
       stack<int> set;
       while (cnt < 4)
       {
           if (i & (1 << cnt))
               set.push(inputSet[cnt]);
           cnt++;
       }
       myPowerSet.push(set);
   }

   return 0;

}

Here myPowerSet contains all the subsets of the input set.

The time complexity of this solution will be 4 * n* n where n is the length of the input Set.

Add a comment
Know the answer?
Add Answer to:
Q2. Answer the following questions assignment, you will develop a well-documented pseudocode that generates all possibl...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT