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
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;
}


C++ program which partitions n positive integers into two disjoint sets with the same sum. Consider...
Challenge activity: A partition of a positive integer n is the expression of n as the sum of positive integers, where order does not matter. For example, two partitions of 7 are 7 1+1+1+4 and 7=1+1+1+2+2. A partition of n is perfect if every integer from 1 to n can be written uniquely as the sum of elements in the partition. 1+1+1+4 is perfect since 1-7 are expressed only as 1, 1+1, 1+1+1, 4, 1+4, 1+1+4 and 1+1+1+4, but 1+1+1+2+2...
(5) Separate N into two disjoint sets: the evens E, and the odds O. Consider the set of Fibonacci ). Prove (n F and En F are infinite sets,6 numbers {1, 1, 2, 3, 5, 8, 13x13 21x21 8x8 Figure 1.10: An interesting geometric proof could use a patterns of the Fibonacci spiral, although there are simpler proofs. the
(5) Separate N into two disjoint sets: the evens E, and the odds O. Consider the set of Fibonacci ). Prove...
C++ please
Question 4: 125 points] Write and test a program that reads two positive integers nl and n2 with n2 > nl. The program then calculates the sum of the prime numbers, using is prime () function developed above, between nl and n2 (inclusive) A Sample input Enter values for nl and n2 (nl<n2): 3 9 Output: Thé Sum of prime numbers between 3 and 9 (inclusive) is 15
Consider the following two problems: Bin Packing: Given n items with positive integer sizes s1, s2, . . . , sn, a capacity C for bins and a positive integer k, is it possible to pack the n items using at most k bins? Partition: Given a set S of n integers, is it possible to partition S into two subsets S1 and S2 so that the sum of the integers in S1 is equal to the sum of the...
2. Write a Marie program that accepts two positive (inputs) two integers and outputs the sum of both of the integers and all the numbers in between Write a Marie program that determines the largest of a series of positive integers provided by a user. The user will enter a -1 when she is finished. Up to 10 numbers will be provided by the user. Write a Marie program that accepts two positive integers, multiples them by repeated addition, and...
Write a C program which reads sets of two integers and displays them and their sum. Continue readings sets of integers until their sum is zero, at which point terminate the program.
C programming! Write a program that reads integers until 0 and prints the sum of values on odd positions minus the sum of values on even positions. Let ?1, ?2, … , ??, 0 be the input sequence. Then the program prints the value of ?1 − ?2 + ?3 − ?4 + ⋯ ??. The input is a sequence of integers that always contains at least 0 and the output will be a single number. For example, for input...
Consider the following problem: given n positive integers, separate them into two groups such that adding all the numbers in one group gives the same result as adding all the numbers in the other group. For example, if the numbers are 1, 2, 3, 4, then the two groups could be {1, 4} and {2, 3}, which both sum to 5. For another example, if the numbers are 5, 6, 11, then the two groups could be {5, 6} and...
Question 5. Let r, n be positive integers. 1. (6 pts) Consider the random binary r n matrix M , where each entry is equal to 0 or 1 with probability 1/2 (so each entry follows the Bernoulli random variable with parameter 1/2), and these entries are (jointly) independent random variables. What is the probability that each column in M has at most one entry with 1? 1 2. (*, 4 pts) Let S1, . . . , Srbe identically...
Write a C program that takes a positive integer n as input and prints all the prime numbers from 1 to n. Sample Input/Output 1: Enter your n: 20 Prime number(s) from 1 to 20 : 2 3 5 7 11 13 17 19 Sample Input/Output 2: Enter your n:2Prime number(s) from 1 to 2 : 2