Question

In: Computer Science

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.

All in one C++ file.

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

Solutions

Expert Solution

#include <bits/stdc++.h>
using namespace std;
/// It is the Function that prints the equal sum sets of the array.
void printing(vector<int> array1, vector<int> array2)
{
int i;
cout << "Equal set:";
for (i = 0; i < array1.size(); i++) {
cout << array1[i] << " ";
}
cout << "\n";
for (i = 0; i < array2.size(); i++) {
cout << array2[i] << " ";
}
}
  
/// Utility function to find the sets of the array which
/// have equal sum.
bool findingsets(int arr[], int n, vector<int>& array1,
vector<int>& array2, int sum1, int sum2, int pos)
{
  
/// If entire array is traversed, compare both the sums.
if (pos == n) {
  
/// If both the sums are equal print them
if (sum1 == sum2) {
printing(array1, array2);
return true;
}
  
/// If sums are not equal then return false
else
return false;
}
  
/// Adding the present element to array1.
array1.push_back(arr[pos]);
  
/// Recursive call after adding current element to array1.
bool res = findingsets(arr, n, array1, array2, sum1 + arr[pos],
sum2, pos + 1);
  
/// If this inclusion results has equal sum sets partition then return true.
if (res)
return res;
  
/// If it is not equal then backtrack by removing current element from array1 and include it in array2.
array1.pop_back();
array2.push_back(arr[pos]);
  
/// recursion after including current element to array2.
res = findingsets(arr, n, array1, array2, sum1, sum2 + arr[pos],
pos + 1);
if (res == false)
if (!array2.empty())
array2.pop_back();
  
return res;
}
  
/// Return true if the array can be partitioned into two equal disjoint sets
bool checkposition(int arr[], int n)
{
/// Calculating the sum of all the elements in the array
int sum = 0;
  
for (int j = 0; j < n; j++)
sum =sum+arr[j];
  
/// we can divide the array only if the sum is even.
if (sum % 2 != 0)
return false;
  
/// Declaring two vectors to store the two disjoint sets
vector<int> array1, array2;
  
/// This function checks whether the array can be divided into sets with equal sum
return findingsets(arr, n, array1, array2, 0, 0, 0);
}
// Main Function
int main()
{
   int input;
   cout << "Enter a number: ";
   cin >> input;
int arr[input],i;
for (int i = 0; i < input; ++i)
{
cin >> arr[i];
}
int n = sizeof(arr) / sizeof(arr[0]);
if (!checkposition(arr, n)) {
cout << "Equal set:0";
}
return 0;
}


Related Solutions

For all integers n > 2, show that the number of integer partitions of n in...
For all integers n > 2, show that the number of integer partitions of n in which each part is greater than one is given by p(n)-p(n-1), where p(n) is the number of integer partitions of n.
Write a program that will calculate the sum of the first n odd integers, and the...
Write a program that will calculate the sum of the first n odd integers, and the sum of the first n even integers. Use a Do while loop Use a for loop Here is a sample of how the program should run: How many odd integers would you like to add? 5 Count Odd Integer Sum 1 1 1 2 3 4 3 5 9 4 7 16 5 9 25 The sum of the first 5 odd integers is...
Three positive integers (a, b, c) with a<b<c are called a Pythagorean triple if the sum...
Three positive integers (a, b, c) with a<b<c are called a Pythagorean triple if the sum of the square of a and the square of b is equal to the square of c. Write a program that prints all Pythagorean triples (one in a line) with a, b, and c all smaller than 1000, as well the total number of such triples in the end. Arrays are not allowed to appear in your code. Hint: user nested loops (Can you...
The subset-sum problem is defined as follows. Given a set of n positive integers, S =...
The subset-sum problem is defined as follows. Given a set of n positive integers, S = {a1, a2, a3, ..., an} and positive integer W, is there a subset of S whose elements sum to W? Design a dynamic program to solve the problem. (Hint: uses a 2-dimensional Boolean array X, with n rows and W+1 columns, i.e., X[i, j] = 1,1 <= i <= n, 0 <= j <= W, if and only if there is a subset of...
Find two positive integers such that the sum of the first number and four times the...
Find two positive integers such that the sum of the first number and four times the second number is 100 and the product of the numbers is as large as possible. please double check answer
C++ Create a program which randomly generates 3 sets of x and y Integers and one...
C++ Create a program which randomly generates 3 sets of x and y Integers and one randomly generated Integer. Two of the sets of Integers will represent the end points of a line segment. The other set of Integers and the other Integer will represent the midpoint of a circle and its radius. The coordinates should be randomly generated using a user defined function that returns an Integer value based on from and to parameters; see the function declaration in...
Question Write a C program that asks the user to enter two integers x and n....
Question Write a C program that asks the user to enter two integers x and n. Then the program computes xn (=x * x * x …… (n times)) using for loop. and give me an output please use printf and scanf #include int main(void) {     //Declare required variables             //read two integers x , n from the keyboard                 //compute xn using for loop                     printf("< Your name >\n");...
1. Write MIPS assembly code to sum “n” positive integers. Forexample, n=5, read 5 numbers...
1. Write MIPS assembly code to sum “n” positive integers. For example, n=5, read 5 numbers in memory (“.data” section) and add them together. 2. Write MIPS assembly code to calculate N-factorial. Read n from the memory. (Hint: use “.data” to define the content in a memory location)
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers...
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers between a given range (limit your range from 0 to 50). For example, if the user want to add the integers between (and including) 1 and 10, then the program should add: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 Algorithm: Program title/description Ask the user to input a start value in the...
Which positive integers n, where 20 ≤ n ≤ 30, have primitive roots?
Which positive integers n, where 20 ≤ n ≤ 30, have primitive roots?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT