## Adding and subtracting a set of numbers to reach a value

From a vector of float numbers

```
std::vector<float> v { 0.32, 0.0004, 12.78, -9.2, 1.1 };
```

I am trying to find out what series of "+" and "-" one can place in front of each float number to get a result that is as close as possible as the value `GOAL`

. Note that no number can be left out (all values must be used).

```
float GOAL = 4.9;
```

For this example, I think the best possible solution is

```
+ 0.32 - 0.0004 + 12.78 + (-9.2) + 1.1 = 4.9996
```

Let `true`

mean "+" and `false`

mean "-", the best possible solution could be represented as

```
std::vector<bool> solution { true, false, true, true, true }
```

One could just iterate through all possible combinations. If `n`

is the size of `v`

, then there are 2^n possible combinations. As `n`

grows large, the process becomes quickly very slow (2^1000 ≈ 10^301).

**How could I go about writing a search algorithm that would output not the best but a descent solution in polynomial time?**

FYI, I have only basic understanding of search algorithms. I understand the concepts of heuristic, greedy algorithm, hill climbing, search tree, minimax game tree and others for examples.

Show source

## Answers to Adding and subtracting a set of numbers to reach a value ( 5 )

not sure if this conforms to your polynomial time requirement, but genetic algorithms tend to do pretty well in this kind of optimization.

also, as an implementation detail, since you are going to add up a large number of floating point numbers, you might want to look into Kahan summation to minimize floating point error.

I am just giving a basic algorithm to do this.

1) Calculate the length of available float numbers. ( I assumed length is fixed ).

2) Have an array of the (length-1). with all zeros. 3) Then try to perform operation between the floating numbers.( Zero refers negative ). 4) If it was not matching to GOAL, then increment the number by assuming the array as binary one. 5) Repeat step 3 & 4 until it matches GOAL. 6) Even at end if it is not matched , there is no possibility.

Ex : Floating vector size is 5. Then the all the possible operations are

Step 2: 0000 --> (1st - 2nd - 3rd - 4th - 5th)

Step 3: 0001 --> (1st - 2nd - 3rd - 4th + 5th)

(Incremented binary num)Step 4: ((1st - 2nd - 3rd - 4th + 5th) != GOAL ) --> Increment and call Step3. So, 0010

It will calculate via all the possibility.

See if this is sufficient:

Now the above code sorts the array in descending order of the values given in the pairs. Note that it sorts based on absolute magnitude. So 10,8,-9 will be sorted as 10,-9,8. Since, after sorting the original positions change, keys are assigned to them, which are nothing but array index.

The solution array is the same as your solution vector.

If the sum is smaller than the goal, the absolute value of the current pair in the vector is added to the sum. Else the absolute value is subtracted.

I don't see an elegant solution but... the following is based on a recursive function (a template function, so you can use it with

`double`

and`long double`

without changes)We can think about a greedy algorithm, which gives a descent solution in

O(n)time.Algorithm:Let the array and goal be :

Now start iterating the vector from the first index, and greedily choose the sign, i.e.

Now since we want the

to be as close to the Goal we will greedily choose "+" for first float.`ValueTillNow`

Now go similarly for rest index in array. Update

. Calculate the diff for two options i.e.`ValueTillNow`

`"+"`

and`"-"`

and choose the one with leads closer to GOAL.Time Complexity : O(n)