minimum number of bins to segregate elements

Viewed 39

Given a list of positive integer elements and a maximum sum(maxSum), we group the elements into bins or buckets provided the sum is less than the maxSum. How to group the elements in the list such that the number of such bins is minimum.

Note : The list may have duplicate elements and it is not sorted.

e.g.

I/P List [1, 1, 2, 4, 4, 7] MaxSum : 9

O/P Minimum Buckets : 3, [1,4,4] [1,4,4] [2,7]

I can only think of a crude solution using sorting O(nlogn) and then iterating over the array starting from the largest element along with a hashmap to track already processed elements O(n*n).

Any insights are welcome.

0 Answers
Related