How to divide a set into two subsets such that difference between the sum of numbers in two sets is minimal?

前端 未结 18 732
攒了一身酷
攒了一身酷 2020-11-27 10:55

Given a set of numbers, divide the numbers into two subsets such that difference between the sum of numbers in two subsets is minimal.

T

18条回答
  •  心在旅途
    2020-11-27 11:31

    HI I think This Problem can be solved in Linear Time on a sorted array , no Polynomial Time is required , rather than Choosing Next Element u can choose nest two Element and decide which side which element to go. in This Way
    in this way minimize the difference, let suppose
    {0,1,5,6} ,
    choose {0,1}
    {0} , {1}
    choose 5,6
    {0,6}, {1,5}
    but still that is not exact solution , now at the end there will be difference of sum in 2 array let suppose x
    but there can be better solution of difference of (less than x)
    for that Find again 1 greedy approach over  sorted half sized array
    and move x/2(or nearby) element from 1 set to another or exchange element of(difference x/2) so that difference can be minimized***
    

提交回复
热议问题