Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7579913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:47:15+00:00 2026-05-30T17:47:15+00:00

I want to group an arbitrary-sized array of random values into n groups, such

  • 0

I want to group an arbitrary-sized array of random values into n groups, such that the sum of values in any one group/bin is as equal as possible.

So for values [1, 2, 4, 5] and n = 2, the output buckets should be [sum(5+1), sum(4+2)].

Some possibilities that occur to me:

  • Full exhaustive breadth first search
  • Random processes with stopping conditions hard coded
  • Start from one end of the sorted array, grouping until the sum is equal to the global average, and move to the next group until n is reached

Seems like the optimal solution (where the sum of the contents of the bins are as equal as possible given the input array) is probably non-trivial; so at the moment I’m leaning towards the last option, but have the feeling I am possibly missing more elegant solutions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:47:17+00:00Added an answer on May 30, 2026 at 5:47 pm

    This is an NP-hard problem. In other words, it’s not possible to find an optimal solution without exploring all combinations, and the number of combinations is n^M (where M is the size of you array, and n the number of beans). It’s a problem very similar to clustering, which is also NP-hard.

    If your data set is small enough to deal with, a brute force algorithm is best (explore all combinations).

    However, if your data set is big, you’ll want a polynomial-time algorithm that won’t get you the optimal solution, but a good approximation. In that case, I suggest you use something similar to K-Means…

    Step 1. Calculate the expected sum per bin. Let A be your array, then the expected sum per bin is SumBin = SUM(A) / n (the sum of all elements in your array over the number of bins).

    Step 2. Put all elements of your array in some collection (e.g. another array) that we’ll call The Bag (this is just a conceptual, so you understand the next steps).

    Step 3. Partition The Bag into n groups (preferably randomly, so that each element ends up in some bin i with probability 1/n). At this point, your bins have all the elements, and The Bag is empty.

    Step 4. Calculate the sum for each bin. If result is the same as last iteration, exit. (this is the expectation step of K-Means)

    Step 5. For each bin i, if its sum is greater than SumBin, pick the first element greater than SumBin and put it back in The Bag; if its sum is less than SumBin, pick the first element less than SumBin and put back in The Bag. This is the gradient descent step (aka maximization step) of K-Means.

    Step 6. Go to step 3.

    This algorithm is just an approximation, but it’s fast and guaranteed to converge.

    If you are skeptical about a randomized algorithm like the above, after the first iteration when you are back to step 3, instead of assigning elements randomly, you can do so optimally by running the Hungarian algorithm, but I am not sure that will guarantee better over-all results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to take any arbitrary SELECT TOP(X) query that would
I want to group the common methods in one file and use it. For
I have some data that has various attributes and I want to hierarchically group
I want to use an existing user and user group table that the forums
I want a group of buttons where a user can choose one of them
I want to group by a varchar column and find the foreignkey value that
I have one table that stores values with a point in time: CREATE TABLE
I want to group results of query by the item id and count number
I want to group by on dict key >>> x [{'a': 10, 'b': 90},
How can I using group by with union in T-SQL? I want to group

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.