Getting the lowest possible sum from numbers' difference

后端 未结 10 1323
滥情空心
滥情空心 2020-12-23 22:19

I have to find the lowest possible sum from numbers\' difference.

Let\'s say I have 4 numbers. 1515, 1520, 1500 and 1535. The lowest sum of difference is 30, becaus

10条回答
  •  既然无缘
    2020-12-23 22:47

    I would go with answer of marcog, you can sort using any of the sorting algoriothms. But there is little thing to analyze now.

    If you have to choose R numbers out N numbers so that the sum of their differences is minimum then the numbers be chosen in a sequence without missing any numbers in between.

    Hence after sorting the array you should run an outer loop from 0 to N-R and an inner loop from 0 to R-1 times to calculate the sum of differnces.

    If needed, you should try with some examples.

提交回复
热议问题