Google | SDE-III | Best way to distribute array elements into k-buckets with a given condition
Anonymous User
609

Hi everyone,

I came across the following question:
Given an array (not sorted) of size N (1 <= N <= 1000) containing values in range 1 <= arr[i] <= 1000000. We have to distribute these elements into k-buckets (1 <= k <= 20) such that

  1. Bucket with maximum sum of elements is minimum
  2. Bucket with maximum number of elements is also minimum

If I sort the array and start placing the elements in buckets starting from largest elements in modulo fashion, then number of elements in all the buckets are almost same. But if the array values are skewed, sum of elements in some of the buckets are very large compared to other bucket.

Instead of sorting, I thought of using min-heap but could not come up with the solution.

Any suggestions?

Comments (3)