given N absolute values of integers find the combination of N/2 negative and N/2 positive values whose sum is closest to 0

后端 未结 5 1432
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 04:36

Let\'s say that I have an array of 10 numbers whose absolute value range can go from 1 to 10. Values can be repeated. An example of this could be

{2, 4, 2, 6, 9         


        
5条回答
  •  温柔的废话
    2021-01-21 05:08

    Have you tried computing differences? Ie: Take the first number. Find the value with the lowest difference, and sum. Continue until finished. In the worst case, the Algorithm is O(n^2) complexity, which isn't exactly desirable but it's a starting point

提交回复
热议问题