Question
Hi please help with this program C++ language thanks
Write a recursive solution to solve the Partition
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// C code to determine if there exist a subset such that sum of elemnets of subset is half of sum of total set
#include <stdio.h>
#include <stdbool.h>

bool partitionSum(int array[], int size, int subsetSum)
{
  
int i,j;
// result[i][j] = 1 if there is a array[0..j-1] with sum = i
bool result[subsetSum+1][size+1];

// If subsetSum is 0, then answer is 1
for (i = 0; i <= size; i++) result[0][i] = 1;

// If subsetSum is not 0 and array is empty, then answer is 0
for (i = 1; i <= subsetSum; i++)
result[i][0] = 0;

// filling the result table
for (i = 1; i <= subsetSum; i++)
{
for (j = 1; j <= size; j++)
{
result[i][j] = result[i][j-1];
if (i >= array[j-1])
result[i][j] = result[i][j] || result[i - array[j-1]][j-1];
}
}

// return the value of subsetSum,size in result table
return result[subsetSum][size];
}

int main()
{
int i,sum = 0;

int array1[] = {5, 10, 6, 8, 1};
int n = sizeof(array1)/sizeof(array1[0]);
  
for (i = 0; i < n; ++i)
{
sum = sum + array1[i];
}
  
int subsetSum = sum/2;
  
if (partitionSum(array1, n, subsetSum) == 1)
printf("Yes, Subset exist\n");
else
printf("Subset does not exist\n");
// output: Yes, Subset exist

sum = 0;

int array2[] = {1, 5, 7, 34, 76, 54, 23, 19, 22, 81, 44, 77, 29, 40, 11, 42, 43, 31, 57, 61};
n = sizeof(array2)/sizeof(array2[0]);
  
for (i = 0; i < n; ++i)
{
sum = sum + array2[i];
}
  
subsetSum = sum/2;

if (partitionSum(array2, n, subsetSum) == 1)
printf("Yes, Subset exist\n");
else
printf("Subset does not exist\n");
// output: Yes, Subset exist

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Hi please help with this program C++ language thanks Write a recursive solution to solve the...
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
  • Describe a non-recursive algorithm that takes a list of distinct integers a_1, a_2, ...., a_n and...

    Describe a non-recursive algorithm that takes a list of distinct integers a_1, a_2, ...., a_n and finds the sum of the primes in the list. Write your answer in pseudo-code or any well-known procedural language like Python, Java, C++, ..... You do not need to write a function to determine whether a number is prime. Assume it is part of your language. E.g. For the list 2, 3, 4, 5, 6, 7, your program should return 17 (because 2 +...

  • Program has to be in C# language Write a computer program that takes two sets (let...

    Program has to be in C# language Write a computer program that takes two sets (let the user determine the cardinality of each set and enter them manually) and calculates the following operations: Union Intersection Difference (set1 - set2) Cartesian product (set2 X set1) Check whether set2 is a subset of set1 or set1 is a subset of set2 Find the powerset of set1 and print out its elements and cardinality

  • INTEL 80x86 ASSEMBLY LANGUAGE CODE Write a windows32 assembly language program that utilizes a recursive procedure....

    INTEL 80x86 ASSEMBLY LANGUAGE CODE Write a windows32 assembly language program that utilizes a recursive procedure. The main (_MainProc) procedure should: accept, from the user, a positive integer. Guard against non-positive integers being entered using a loop. call the sumseries sub-procedure using the cdecl protocol, receive the results of the sub-procedure, and display the results. The sumseries sub-procedure should: recursively find the sum of the series: 1*2 + 2*3 + 3*4 + ... + i*(i+1) (This is an iterative definition....

  • Please solve this question using c++ language Problem 2. More Probleml. The program builds the array...

    Please solve this question using c++ language Problem 2. More Probleml. The program builds the array of integers that contains duplicates as well. Your program should find the sum of all unique elements in the array using a function findUniqueSum Arrays Write a program that as before asks the user to enter input as in оn Sample Input/Output Enter a list of numbers ending with -999 3 1 4 55-999 The sum of unique numbers is: 13 Enter a list...

  • Programming Language: Java Please write a program that uses a recursive algorithm to compute the determinant...

    Programming Language: Java Please write a program that uses a recursive algorithm to compute the determinant of a matrix. It should read the order of the matrix, read the matrix, print it out, compute, and print the determinant. Your program should be able to evaluate multiple matrices on a single execution. For class purposes, your program should handle matrices up to and including those of order 6. You are required to use an array for this problem. Your solution must...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list : You are given a sequence of n positive integers a1,a2,…,an, where n is even. Swap adjacent elements in the given sequence and print the resulting sequence: a2,a1,a4,a3,a6,a5,… Input The first line contains a positive even integer n (2≤n≤1000) — the length of the sequence. The second line contains n space-separated integers a1,a2,…,an (1≤ai≤1000) — the elements of the sequence. Output Print n...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list or string or loops (while and for) or if,elif,else : You are given a sequence of n non-zero integers a1,a2,…,an. Determine if the given sequence contains a pair of adjacent elements of the same sign (both negative or both positive). Two elements are called adjacent if they stand next to each other in the sequence. Input The first line contains an integer n...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list : You are given a non-decreasing sequence of n positive integers a1,a2,…,an. Print the number of distinct values in the sequence. For example, if the sequence is 1,2,2,2,3,4,4,5,7,10, the answer is 6 since distinct values are 1,2,3,4,5,7,10. Input The first line contains a positive integer n (1≤n≤1000) — the length of the sequence. The second line contains n space-separated positive integers a1,a2,…,an (1≤ai≤1000)...

  • Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a...

    Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a single BIG integer (bigint) as its parameter n, and it will compute the factorial n! of the value and return it as its result (as a bigint). Write this functions using a loop (do not use recursion). 2. Write the factorial function again, but using recursion. Call this function factorialRecursive(). The function takes the same parameters and returns the same result as described in...

  • Please help C++ language Instructions Write a program that prompts the user to enter 50 integers...

    Please help C++ language Instructions Write a program that prompts the user to enter 50 integers and stores them in an array. The program then determines and outputs which numbers in the array are sum of two other array elements. If an array element is the sum of two other array elements, then for this array element, the program should output all such pairs.

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