Program done in PROLOG
Print equal sum sets of array (Partition problem)
Given an array arr[]. Determine whether it is possible to split the array into two sets such that the sum of elements in both the sets is equal. If it is possible, then print both the sets. If it is not possible then output -1.
Code is as follows-
// CPP program to print equal sum sets of array. #include <bits/stdc++.h> using namespace std; // Function to print equal sum // sets of array. void printEqualSumSets(int arr[], int n) { int i, currSum; // Finding sum of array elements int sum = accumulate(arr, arr+n, 0); // Check sum is even or odd. If odd // then array cannot be partitioned. // Print -1 and return. if (sum & 1) { cout << "-1"; return; } // Divide sum by 2 to find // sum of two possible subsets. int k = sum >> 1; // Boolean DP table to store result // of states. // dp[i][j] = true if there is a // subset of elements in first i elements // of array that has sum equal to j. bool dp[n + 1][k + 1]; // If number of elements are zero, then // no sum can be obtained. for (i = 1; i <= k; i++) dp[0][i] = false; // Sum 0 can be obtained by not selecting // any element. for (i = 0; i <= n; i++) dp[i][0] = true; // Fill the DP table in bottom up manner. for (i = 1; i <= n; i++) { for (currSum = 1; currSum <= k; currSum++) { // Excluding current element. dp[i][currSum] = dp[i - 1][currSum]; // Including current element if (arr[i - 1] <= currSum) dp[i][currSum] = dp[i][currSum] | dp[i - 1][currSum - arr[i - 1]]; } } // Required sets set1 and set2. vector<int> set1, set2; // If partition is not possible print // -1 and return. if (!dp[n][k]) { cout << "-1\n"; return; } // Start from last element in dp table. i = n; currSum = k; while (i > 0 && currSum >= 0) { // If current element does not // contribute to k, then it belongs // to set 2. if (dp[i - 1][currSum]) { i--; set2.push_back(arr[i]); } // If current element contribute // to k then it belongs to set 1. else if (dp[i - 1][currSum - arr[i - 1]]) { i--; currSum -= arr[i]; set1.push_back(arr[i]); } } // Print elements of both the sets. cout << "Set 1 elements: "; for (i = 0; i < set1.size(); i++) cout << set1[i] << " "; cout << "\nSet 2 elements: "; for (i = 0; i < set2.size(); i++) cout << set2[i] << " "; } // Driver program. int main() { int arr[] = { 5, 5, 1, 11 }; int n = sizeof(arr) / sizeof(arr[0]); printEqualSumSets(arr, n); return 0; }
Output-

If the answer helped then please upvote, it means a
lot.
And for any queries feel free to comment.
Program done in PROLOG Print equal sum sets of array (Partition problem) Given an array arr[]....
Given an array A[] of integers find sum of product of all pairs of array elements Input: First line consists of T test cases. First line of every test case consists of N, denoting number of elements of array. Second line consists of elements of array. Output: Single line output, print the sum of products. Constraints: 1<=T<=100 1<=N<=100 Solve the problem using pointers and (if possible) using operators Example: Input: 2 3 1 3 4 4 2 3 4 5...
Consider the following code C++ like program: int i, j, arr[5]; //arr is an array starting at index 0 void exchange(int x, int y) { int temp:= x; x:= y; y:= temp; } main(){ for (j = 0; j < 5; j++) arr[j]:= j; i:= 1; exchange(i, arr[i+1]); output(i, arr[2]); //print i and arr[2] } What is the output of the code if both parameters in function swapping are passed by: a- value? b- reference? c- value-result?
Show that PARTITION is
NP-complete by reduction from SUBSET-SUM.
Given a set of integers, we say that can be partitioned if it can be split into two sets U and V so that considering all u EU and all v € V, Eu = Ev. Let PARTITION = { <S> S can be partitioned ). Show that PARTITION IS NP-complete by reduction from SUBSET-SUM.
c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...
Write a C++ program to scale and average the values in a two-dimensional array. Write the following functions: void randomize2DArray(int arr[ROW_SIZE][COL_SIZE]) – places random values between 1 and 100 in a two-dimensional array . void scale2DArray(int arr[ROW_SIZE][COL_SIZE], double scale) – multiplies every value in the two-dimensional array by scale. double average2DArray(int arr[ROW_SIZE][COL_SIZE]) – calculates the average value over all elements in the two-dimensional array. Randomize and print the values in the two-dimensional array of size 7 x...
(JAVA) Given an array of unique positive integers, write a function findSums that takes the array input and: 1. Creates a hashtable called “hashT” and inserts all the elements of the input array in the hashtable. 2. Finds pairs of elements in the hashtable whose sum is another element (sum) in the hashtable and print the pairs in the console. 3. Returns another hashtable names “sums” of those sum elements. For example: Input: [1,5,4,6,7,9] Output: [6,5,7,9] Explanation: 6 = 1...
C# : Write a program which calculates Sum, Average of a given array. Array must be initialized using initializer. use loops to calculate the same display the output.
Write a small program that uses a function to add array elements from an array into a linked list in-order. Use the following array: const int size=5; int arr[size] = { 4, 1, 7, 2 ,3 }; Output: Array out of order: 4 1 7 2 3 Array in order using Linked List : 1 ,2, 3, 4, 7 Your program should display the linked list in-order using the array given.
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
Java 8
9m left Jav 27 28 ALL 29 0 Given an integer array, separate the values of the array into two subsets, A and B, whose intersection is null and where the addition of the two subsets equals the entire array. The sum of values in set A must be strictly greater than the sum of values in set B, and the number of elements in set A must be minimal. Return the values in set A in increasing...