Question

C++ program which partitions n positive integers into two disjoint sets with the same sum. Consider...

C++ program which partitions n positive integers into two disjoint sets with the same sum. Consider all possible subsets of the input numbers.

This is the sample Input 1

6
3 5 20 7 1 14

Output 1

Equal Set: 1 3 7 14

This is the sample Input 2

5
10 8 6 4 2

Output 2

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

Main.cpp file :

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

class Set {
private:
   class node {
   public:
       int data;
       node* next;
       node(int _data) {
           data = _data;
           next = nullptr;
       }
   };
public:
   node* head = nullptr;
   Set(); // default constructor
   ~Set(); // destructor
   int sum(); // return sum of all elements in the set
   void add(int x); // adds an element to end of set
   void copy(Set& s); // returns a copy of set
   string toString(); // returns a string of elements in subset separated by sapce
};


Set::Set() {
   head = nullptr;
}

Set::~Set() {
   while (head != nullptr) {
       node* temp = head;
       head = head->next;
       delete temp;
   }
}

int Set::sum() {
   int total = 0;
   node* itr = head;
   while (itr != nullptr) {
       total = total + itr->data;
       itr = itr->next;
   }
   return total;
}

void Set::add(int x) {
   if (head == nullptr) {
       head = new node(x);
   }
   else {
       node* itr = head;
       while (itr->next != nullptr) {
           itr = itr->next;
       }
       itr->next = new node(x);
   }
}

void Set::copy(Set& s) {
   node* itr = s.head;
   while (itr != nullptr) {
       this->add(itr->data);
       itr = itr->next;
   }
}

string Set::toString() {
   string str = "";
   node* itr = head;
   while (itr != nullptr) {
       str = str + to_string(itr->data) + " ";
       itr = itr->next;
   }
   return str;
}

int main() {
   cout << "Enter number of inputs followed by inputs separated by a space:" << endl; //prompt for input
   int num_of_input; //number of elements in the set
   string input; //input elements separated by space
   // get input from user
   cin >> num_of_input;
   cin.ignore();
   getline(cin, input);

   // for n element in the set there are 2^n-1 posible subsets
   int num_subset = pow(2, num_of_input) - 1;
   // create an array to hold the subsets
   Set* subsets = new Set[num_subset];
   // create universal set from input
   int* universal = new int[num_of_input];
   stringstream ss;
   ss << input;
   for (int i = 0; i < num_of_input; i++) {
       ss >> universal[i];
   }
   // get total sum of universal set
   int total_sum = 0;
   for (int i = 0; i < num_of_input; i++) {
       total_sum = total_sum + universal[i];
   }
   // disjoint sets should have half the sum of total
   int sum = total_sum / 2;
   // create subsets
   for (int i = 0; i < num_of_input; i++) {
       int offset = pow(2, i) - 1;
       subsets[offset].add(universal[i]);
       for (int j = 0; j < offset; j++) {
           subsets[offset + j + 1].copy(subsets[j]);
           subsets[offset + j + 1].add(universal[i]);
       }
   }
   // print
   cout << "Equal Set: ";
   // set flag
   bool found = false;
   // check for subsets's sum
   for (int i = 0; i < num_subset; i++) {
       if (subsets[i].sum() == sum) {
           cout << subsets[i].toString() << endl;
           found = true;
       }
   }
   if (!found) {
       cout << 0;
   }
   delete[] subsets;
}

Add a comment
Know the answer?
Add Answer to:
C++ program which partitions n positive integers into two disjoint sets with the same sum. Consider...
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