Questions tagged [subset-sum]

In computer science, the subset sum problem is one of the important problems in complexity theory and cryptography.

In computer science, the subset sum problem is one of the important problems in complexity theory and cryptography.

424 questions
2
votes
1 answer

Recovering Subsets in Subset Sum Problem - Not All Subsets Appear

Brushing up on dynamic programming (DP) when I came across this problem. I managed to use DP to determine how many solutions there are in the subset sum problem. def SetSum(num_set, num_sum): #Initialize DP matrix with base cases set to 1 …
Macroxela
  • 43
  • 4
2
votes
1 answer

(Prolog) Check if a list can be split into 2 sub-lists that have equal sums

I am using Prolog to try and check if a list can be split into 2 sublists(subarrays) that have equal sums. The following should succeed: [1,2,3,6], [2,1,1], [0], [1,1,2] The following should fail: [1,4,8], [1,3,2], [2,2,1,1] I believe my program is…
Harrison Bergman
  • 169
  • 1
  • 12
2
votes
3 answers

How to divide a list of negative and positive numbers into the largest number of subsets whose sum is 0?

I am trying to solve this problem but I can't manage to figure out how. Let's suppose I have a list of positive and negative numbers whose sum is guaranteed to be 0. [-10, 1, 2, 20, 5, -100, -80, 10, 15, 15, 60, 100, -20, -18] I…
reuseman
  • 323
  • 1
  • 6
  • 19
2
votes
2 answers

Solving the Subset Sum Problem using Recursive Linked Structures

Imagine a non-ordered singularly-linked structure with integer nodes (ex. 5 -> 8 -> 10 -> 2 -> 7), and a given sum (ex. 15). How could I find the first subset to equate to the sum recursively? In this example, the answer would be 5 -> 8 -> 2 (5 ->…
iso
  • 31
  • 7
2
votes
1 answer

Subset sum of P elements (with repetition allowed) divisible by M

Given an array A of N elements. We need to find the number of subsets (with repetition of numbers allowed) such that the number of elements in the subset is P and the sum of those P elements is divisible by M. N can be upto 10^5 P can be upto 10^5 M…
2
votes
1 answer

Minimize or find a n ideal subset-sums from a list of numbers which are less than or equal to some N

Given a list of numbers, lst = [1, 1, 2, 4, 3, 2, 1, 1, 1, 2, 1, 4, 3, 1] how would I find an ideal number of lists which are less than or equal to 4? There are many possibilities here. The goal is to minimize the number of possible lists. The…
GeneralCode
  • 794
  • 1
  • 11
  • 26
2
votes
1 answer

Bounded subset sum

I have a question about a variant subset-sum problem. In a set S = {N1, N2, N3... Ni}, each element can be selected multiple time with upper limit = { L1, L2, L3... Li}. And the question is, what is the most efficient algorithm to find one subset…
2
votes
1 answer

How to find N numbers whose sum is closest to K , but over multiple columns?

I'm trying to solve an optimization problem which consists of finding, an optimal solution to the subset sum problem but, we need to find a solution in which sum of each column is closest to a unique number for each column. Another constraint is…
2
votes
2 answers

How to express 2n as sum of n variables (Java implementation?)

I wonder if there is an elegant way to derive all compositions of 2n as the sum of n non-negative integer variables. For example, for n = 2 variables x and y, there are 5 compositions with two parts : x = 0 y = 4; x = 1 y = 3; x = 2 y = 2; x = 3 y =…
skyork
  • 7,113
  • 18
  • 63
  • 103
2
votes
2 answers

How to implement the Sum of Subsets problem in Java

Does anyone know how to implement the Sum-of-Subsets problem in Java from this pseudo code? w = an array of positive integers sorted in non-decreasing order. W = the target sum value include = an array or arraylist of the solutions who's weight adds…
Stylzs05
  • 491
  • 2
  • 8
  • 19
2
votes
2 answers

Python Subset Sum Problem for Given Length of Elements

For given set, and sum, and length of elements, I want to get the boolean value whether the set satisfy the condition For example... Input : set = [18,0,2,20], sum = 20, length = 2
Output : True (subset [18,2] satisfy the sum=20 for given…
MTHL
  • 123
  • 5
2
votes
1 answer

Subset Sum using Bactracking

I was trying to solve the following problem using backtracking : Let us say that you are given a number N, you've to find the number of different ways to write it as the sum of 1, 3 and 4. Here is my attempt: const backtrack = (array, index,…
2
votes
2 answers

Scheme Syntax Help: Using a function that I have defined in another program

I have created two functions to help me solve my Subset sum problem. I seem to be getting an error though. It tells me that I am passing two arguments to list-sum. I've been fooling around with this program for several hours now. I was wondering if…
user614885
  • 141
  • 2
  • 2
  • 9
2
votes
2 answers

Split a subset with a constraint

Today, while practicing some Algorithm questions I found an interesting question. The question is You have to divide 1 to n (with one missing value x ) into two equal halfs such that sum of the two halfs are equal. Example: If n = 7 and x =…
2
votes
5 answers

Why was my algorithm for this interview a sub-optimal approach?

Given a list of integers, l = [1,5,3,2,6] and a target t = 6, return true if the list contains two distinct integers that sum to the target I was given this question on a technical Python interview that caused me not to pass. My answer was: def…