1

Given a sorted list of unique positive numbers, find the order in which these numbers should be added or subtracted such that with each calculation, the total should be positive or negative according to given instructions that consist of +/- signs.
The number of instructions will match the number of numbers given, each number can only be used once, and the total can't be zero when adding or subtracting numbers

Example:
Numbers: 8 19 56 65 73 79 87
Instructions: - + - - - - +
Possible Solution: -79 +87 -19 +8 -65 +56 +73

I have been stuck on this problem and can't figure out a method for the problem that could cover all cases. I can't seem to figure out how to find which number should be used first as I might run out of numbers that would be create a valid next total. I have made many random sets of numbers and tried applying different sets of instructions to them but I can't find any reliable trends or methods that will help me solve this problem. What I've thought about so far is that if I want to keep a number negative for example, I should add the greatest number possible that would still sum a negative number. And if I wanted to keep it negative again, I would subtract a number smaller than the number that was just added. The same would apply to positives as well but with adding and subtracting reversed.

For example: Given 1 2 3 4 and - - - +
I would subtract 3 to create the first negative, add 2 to keep it negative while trying to get it close enough to zero (if I were to subtract and the next instruction was + then it'd be impossible to reach), subtract 1 to stay negative, and finally add 4 to follow the +.
The answer would be: -3 +2 -1 +4


I don't mean to look for someone to do my homework, I just want some help to guide me in the right direction because I'm feeling really helpless on this one for some reason. This is a problem I am coding in python but I don't even know how to start so I would just appreciate conceptual help.

  • 4
    This reaaaaally looks like a homework problem. What have you tried? Please post your code. – James Nov 24 '19 at 22:11
  • I suggest making a list of the differences between the available numbers, and use those differences to guide your choices. – Bob Jarvis - Слава Україні Nov 24 '19 at 22:43
  • Sorry about that, I do not have code yet but I have thought of starting with the middle number and alternating the signs of the number list (-1, +2, -3, +4). Possibly work towards one side and when the instructions switch from sign to sign then I would use the other side of the list to hopefully reach a valid total. This didn't work all the time unfortunately but I feel like it is still somewhat useful – someStuckGuy Nov 24 '19 at 22:47
  • Would the differences be between the 1st and go through the list one by one, then the 2nd and the rest of the list one by one? – someStuckGuy Nov 24 '19 at 22:48

1 Answers1

0

You could always brute force it using a tree structure. Build a tree containing all permutations of your numbers (positive and negative), with the root set to 0.

Then use a depth-first traversal to add these numbers, checking if the result matches the expected sign along the way. Backtrack and drop the subtree if it does not match. If you reach a leaf, you have found a solution.

Bastien
  • 658
  • 1
  • 9
  • 24