Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tricky programming problem that I'm having trouble getting my head around

First off, let me say that this is not homework (I am an A-Level student, this is nothing close to what we problem solve (this is way harder)), but more of a problem I'm trying to suss out to improve my programming logic.

I thought of a scenario where there is an array of random integers, let's for example say 10 integers. The user will input a number he wants to count to, and the algorithm will try and work out what numbers are needed to make that sum. For example if I wanted to make the sum 44 from this array of integers:

myIntegers = array(1, 5, 9, 3, 7, 12, 36, 22, 19, 63); 

The output would be:

36 + 3 + 5 = 44 

Or something along those lines. I hope I make myself clear. As an added bonus I would like to make the algorithm pick as few numbers as possible to make the required sum, or give out an error if the sum cannot be made with the numbers supplied.

I thought about using recursion and iterating through the array, adding numbers over and over until the sum is met or gone past. But what I can't get my head around is what to do if the algorithm goes past the sum and needs to be selective about what numbers to pick from the array.

I'm not looking for complete code, or a complete algorithm, I just want your opinions on how I should proceed with this and perhaps share a few tips or something. I'll probably start work on this tonight. :P

As I said, not homework. Just me wanting to do something a bit more advanced.

Thanks for any help you're able to offer. :)

like image 417
Phox Avatar asked Feb 23 '10 17:02

Phox


People also ask

What to do if you are stuck on a programming problem?

Take A Break This is the funniest and most effective solution. But surprisingly, it not only works on me but also on several of my programmer friends and colleagues. Half of my problems were solved after getting a good night's sleep. So if you are stuck in a problem, take a break.

How do I clear my mind for coding?

Weightlifting, running, cycling or any other activity that gets the takes a lot of energy is a great way to get your mind off of the problem. The activity will help get rid of the stress, make you feel better and coincidentally you'll get the benefit of being in better shape.


2 Answers

You are looking at the Knapsack Problem

The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than a given limit and the total value is as large as possible. It derives its name from the problem faced by someone who is constrained by a fixed-size knapsack and must fill it with the most useful items.


Edit: Your special case is the Subset Sum Problem

like image 75
Otto Allmendinger Avatar answered Sep 22 '22 20:09

Otto Allmendinger


Will subset sum do? ;]

like image 21
pablochan Avatar answered Sep 21 '22 20:09

pablochan