4
1
You are given two arbitrary integer lists, e.g. [-10, 99, -6, 43]
, [100, 32, 44]
. You are to pick one integer from each list and sum them up, then sort the best pairs ordered by the absolute distance to 100 in ascending order. Paired up integers from each list shall not be repeated in the output.
For instance, we have
(-10, 100): |100 - (-10 + 100)| = 10
(-10, 32) : |100 - (-10 + 32 )| = 78
(-10, 44) : |100 - (-10 + 44 )| = 66
(99, 100) : |100 - (99 + 100 )| = 99
(99, 32 ) : |100 - (99 + 32 )| = 31
(99, 44 ) : |100 - (99 + 44 )| = 43
(-6, 100) : |100 - (-6 + 100 )| = 6
(-6, 32 ) : |100 - (-6 + 32 )| = 74
(-6, 44 ) : |100 - (-6 + 44 )| = 62
(43, 100) : |100 - (43 + 100 )| = 43
(43, 32 ) : |100 - (43 + 32 )| = 25
(43, 44 ) : |100 - (43 + 44 )| = 13
After pairing with the first item -10
, it is tempted to pick the pair (-10, 100)
as the best pair as it has the least distance to 100. But further checking down the list, (-6, 100)
in fact has a lesser distance to 100, hence it should be the best pair picked. After picking the pair, they are removed from their lists, leaving us
(-10, 32) : |100 - (-10 + 32)| = 78
(-10, 44) : |100 - (-10 + 44)| = 66
(99, 32 ) : |100 - (99 + 32 )| = 31
(99, 44 ) : |100 - (99 + 44 )| = 43
(43, 32 ) : |100 - (43 + 32 )| = 25
(43, 44 ) : |100 - (43 + 44 )| = 13
Then next pair to pick will be (43, 44)
. Notice that the distance to 100 is 13 for this pair, which is higher than the pair (-10, 100)
, but only because 100
has been paired with -6
and hence the pair (-10, 100)
is gone. Moving on, we have these remaining
(-10, 32) : |100 - (-10 + 32)| = 78
(99, 32 ) : |100 - (99 + 32 )| = 31
And finally (99, 32)
is picked. Hence the output (-6, 100), (43, 44), (99, 32)
. Ignore the rest that have nothing to pair up.
Note: when there is a tie, just pick any pair, without worrying the consequence to the remaining candidates.
The challenge here is to complete the task with the least number of comparisons operating on the inputs. i.e. if all comparison result has to come from a function comp(a, b)
, then the least number of call to this function wins. Looping control etc. are not included. Three way comparison is essentially 2 operations. General purpose code only and no special purpose optimisation is required. Favour code that can verify the number of comparison calls, however, when using build in functions, please explain their complexity for us to examine.
1Welcome to PPCG! / Can you clarify if 3-way comparison is counted as 1 or 2 steps? – user202729 – 2018-06-14T10:13:03.180
I assume that the only operation that you can do on the numbers are comparing them (instead of, say, use them to index into arrays, or hash them? Is addition or multiplication allowed?) – user202729 – 2018-06-14T10:14:29.480
4In summary: This is a nice challenge idea, but in the current form it's too unclear. You can sandbox the challenge before posting to main. – user202729 – 2018-06-14T10:39:42.383
@user202729 haha thanks. First time poster, tried my best. Points noted and updated in the question. Cheers. Enjoy! – user1589188 – 2018-06-14T11:33:13.350
2The least number of comparisons over what space of possible inputs? That is, I could write a function that gives correct answers for all inputs, does a provably minimum number of comparisons on this example input, but does way more comparisons than necessary on other inputs. – aschepler – 2018-06-14T11:52:02.240
How is
comp
defined regarding builtin functions? Let's say I calculate the absolute differences as you did, and then take the minimum of the list (with a builtin, not manually). How manycomp
doesminimum
count as? (1? or the size of the list since the builtin compares each under the hood?) – Kevin Cruijssen – 2018-06-14T12:04:32.483