
Calvin D. answered 08/02/22
Data Scientist
There are two ways to answer this question, one which just iterates through the set of numbers and does the comparison above, and one that provides the technically optimal solution by comparing all possible outputs.
Without doing the math, we can visualize the difference mentally, consider this example:
1 2 3 4 5 6 7 8
Let's say we select (1, 2) and (4, 7) from our list, none of the other numbers match the description. We got there by starting at the first number and evaluating everything after it. Again, I'm not actually doing the calculation here, just imagining.
There is also a world where if we instead skipped the first number, we get (2, 3), (4,5), (6,7). That's three pairs - one more than the previous. See the problem? The answer may depend on which numbers you evaluate, in which order. The problem then devolves into a discrete math problem instead of a coding one, where we then go on to evaluate decision trees and other stuff like that.
I'm a computer scientist, not a discrete mathematician, so let's do it the first way and call it a day.
We basically compare every number in the list to every other number in the list, and if it finds a pair, print that pair and then add those two numbers to the blacklist so they can't be used again the next time around.
Technically this produces a solution that makes every number appear as a pair, such as (0, 0) and (1, 1) etc being valid pairs for Teena, since they meet the condition. The problem also does not say a single number can only appear in the pair once, just that it can only be in one pair, so a number could (and does) appear in a single pair twice.
We can fix this by adding an if statement to check if the two numbers are equal, and continue the calculation:
This does change our answer, and gives us 55 entries instead of 110 from the previous version.