What would be the best algorithm to solve this problem? I spent a couple of hours on this problem. But couldn’t sort it out.
A guy purchased a necklace and planned to make it into two pieces in such a way that the average brightness of each piece should be either greater than or equal to the original piece.
The criteria for dividing the necklaces are
1.The difference in number of pearls between the two pearls sets should not be greater than 10% of the number of pearls in the original necklace or 3 whichever is higher.
2.The difference between number of pearls in 2 necklaces should be minimum.
3.In case if the average brightness of any one of the necklace is less than the average brightness of the original set return 0 as output.
4.Two necklaces should have their average brightness greater than the original one and the difference between the average brightness of the two pieces is minimum.
5.The average brightness of each piece should be either greater than or equal to the original piece.
This problem is rather hard to do efficiently (in NP somewhere).
Say you had a set that averaged to
X. That is,X = (x1 + x2 + ... + xn) / n.Suppose you break it up into sets that average to
SandTwithsandtitems in each set, respectively.You can mathematically prove that if one of the averages,
SorT, is greater thanX, the other of the two must be less thanX.Hence, the two sets must have exactly the same brightness because that’s the only way your conditions are satisfiable.
Knowing this, you’re ending up with the sumset sum problem — you want to find a subset that sums to exactly half of the sum of the entire set. That’s a problem that’s known to be hard. (It’s been classified NP. And alright, it’s not exactly the same as the subset sum problem, but if subtract the average of the full set from each of the brightness values, solving the subset sum problem will give you your answer. (Do the reverse to see how you can solve the subset sum problem from your problem.)
Hence, there’s no fast way of doing this — only approximations or exponential running times… However, maybe this will help. It mentions better running times if your weights (in your case, brightness levels) are bounded.