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
6
votes
1 answer

Efficient algorithm to find whether a subset of an integer array exists,the xor of all its elements is a given value?

I have a positive integer array- {1,5,8,2,10} and a given value 7. I need to find whether a subset of the array exists such that the XOR of its elements is the value 7. In this case the subset is {5,2} because 5 xor 2 is 7. One naive solution is to…
user3522401
  • 229
  • 2
  • 10
6
votes
5 answers

The subsets-sum problem and the solvability of NP-complete problems

I was reading about the subset-sums problem when I came up with what appears to be a general-purpose algorithm for solving it: (defun subset-contains-sum (set sum) (let ((subsets) (new-subset) (new-sum)) (dolist (element set) …
G.E.M.
  • 63
  • 4
6
votes
7 answers

Python Subset Sum

I am trying to write a function that will not only determine whether the sum of a subset of a set adds to a desired target number, but also to print the subset that is the solution. Here is my code for finding whether a subset exists: def…
Chase McCoy
  • 1,550
  • 2
  • 13
  • 16
6
votes
3 answers

Subset-Sum in Linear Time

This was a question on our Algorithms final exam. It's verbatim because the prof let us take a copy of the exam home. (20 points) Let I = {r1,r2,...,rn} be a set of n arbitrary positive integers and the values in I are distinct. I is not given…
cjhin
  • 235
  • 4
  • 16
6
votes
2 answers

Haskell generate subsets

I have a function 'subsets' which generate all the subsets of a given set: subsets :: [Int] -> [[Int]] subsets [] = [[]] subsets (x:xs) = subsets xs ++ map (x:) (subsets xs) How can I combine map, foldl and filter in another function to return me…
orion
  • 101
  • 1
  • 4
5
votes
3 answers

Subset sum recursively in Python

I will be happy to get some help. I have the following problem: I'm given a list of numbers seq and a target number and I need to write 2 things: A recursive solution that returns True if there is a sum of a subsequence that equals the target…
5
votes
3 answers

custom partition problem

Could some one guide me on how to solve this problem. We are given a set S with k number of elements in it. Now we have to divide the set S into x subsets such that the difference in number of elements in each subset is not more than 1 and the sum…
yahh
  • 1,175
  • 3
  • 14
  • 41
5
votes
1 answer

"Smart" method to solve a subset sum problem with multiple sets

I have a certain amount of sets, each containing a variable amount of unique numbers - unique in the set they belong to and that can't be found in others. I'd like to make an algorithm implemented preferably in Python - but it can be any other…
CodeTalker
  • 79
  • 6
5
votes
3 answers

Dynamic programming : perfect sum with negative numbers

Given an array of integers and a sum, the task is to print all subsets of given array with sum equal to given sum. Example: Input : arr[] = {1, 2, 3, 4, 5} sum = 10 Output : [4 3 2 1] [5 3 2] [5 4 1] Input : arr[] =…
Somesh Dhal
  • 336
  • 2
  • 15
5
votes
2 answers

Subset sum variant with modulo

Given an array of integers A and integers N, M. I want to find all the subsets S of A where (sum(S) mod M = N). A can have multiple integers of the same value. In my case N will be in the range 0<=n<=31, M will be 32 and A will contain integers in…
5
votes
1 answer

subset which gives the least sum greater than or equal to a given number

I have a (multi)set of positive numbers, eg. {71.28, 82.62, 148.77, 85.05, 50.76, 103.41}. I want to find the subset which gives the least sum greater than or equal to a given number. Eg. if the minimum was 270, then the result would be {148.77,…
boofaz
  • 301
  • 1
  • 8
5
votes
3 answers

All possible permutations of decimal numbers (hundredths) that sum up to 1 for a given length

Consider vector s as follows: s=seq(0.01, 0.99, 0.01) > s [1] 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 .......... 0.89 0.90 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99 Now given s and a fixed length m, I would like to have a matrix for all…
989
  • 12,579
  • 5
  • 31
  • 53
5
votes
1 answer

Fewest subsets with sum less than N

I have a specific sub-problem for which I am having trouble coming up with an optimal solution. This problem is similar to the subset sum group of problems as well as space filling problems, but I have not seen this specific problem posed anywhere.…
cr1msonB1ade
  • 1,716
  • 9
  • 14
5
votes
2 answers

Reduce Subset Sum to Polyomino Packing

This is a homework assignment, so any help is appreciated. I should prove that the following problem is NP-complete. The hint says that you should reduce the subset sum problem to this problem. Given a set of shapes, like the below, and an m-by-n…
Ashkan Kazemi
  • 1,077
  • 8
  • 26
5
votes
5 answers

Find all k-size subsets with sum s of an n-size bag of duplicate unsorted positive integers

Please note that this is required for a C# .NET 2.0 project (Linq not allowed). I know very similar questions have been asked here and I have already produce some working code (see below) but still would like an advice as to how to make the…
Ali Adams
  • 65
  • 1
  • 8
1 2
3
28 29