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

  • SEARCH
  • Home
  • 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 835335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:48:43+00:00 2026-05-15T04:48:43+00:00

I have the following problem: Given N objects (N < 30) of different values

  • 0

I have the following problem:

Given N objects (N < 30) of different values multiple of a “k” constant i.e. k, 2k, 3k, 4k, 6k, 8k, 12k, 16k, 24k and 32k, I need an algorithm that will distribute all items to M players (M <= 6) in such a way that the total value of the objects each player gets is as even as possible (in other words, I want to distribute all objects to all players in the fairest way possible).

EDIT: By fairest distribution I mean that the difference between the value of the objects any two players get is minimal.
Another similar case would be: I have N coins of different values and I need to divide them equally among M players; sometimes they don’t divide exactly and I need to find the next best case of distribution (where no player is angry because another one got too much money).

I don’t need (pseudo)code to solve this (also, this is not a homework 🙂 ), but I’ll appreciate any ideas or links to algorithms that could solve this.

Thanks!

  • 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-15T04:48:43+00:00Added an answer on May 15, 2026 at 4:48 am

    The problem is strongly NP-complete. This means there is no way to ensure a correct solution in reasonable time. (See 3-partition-problem, thanks Paul).

    Instead you’ll wanna go for a good approximate solution generator. These can often get very close to the optimal answer in very short time. I can recommend the Simulated Annealing technique, which you will also be able to use for a ton of other NP-complete problems.

    The idea is this:

    1. Distribute the items randomly.
    2. Continually make random swaps between two random players, as long as it makes the system more fair, or only a little less fair (see the wiki for details).
    3. Stop when you have something fair enough, or you have run out of time.

    This solution is much stronger than the ‘greedy’ algorithms many suggest. The greedy algorithm is the one where you continuously add the largest item to the ‘poorest’ player. An example of a testcase where greedy fails is [10,9,8,7,7,5,5].


    I did an implementation of SA for you. It follows the wiki article strictly, for educational purposes. If you optimize it, I would say a 100x improvement wouldn’t be unrealistic.

    from __future__ import division
    import random, math
    
    values = [10,9,8,7,7,5,5]
    M = 3
    kmax = 1000
    emax = 0
    
    def s0():
        s = [[] for i in xrange(M)]
        for v in values:
            random.choice(s).append(v)
        return s
    
    def E(s):
        avg = sum(values)/M
        return sum(abs(avg-sum(p))**2 for p in s)
    
    def neighbour(s):
        snew = [p[:] for p in s]
        while True:
            p1, p2 = random.sample(xrange(M),2)
            if s[p1]: break
        item = random.randrange(len(s[p1]))
        snew[p2].append(snew[p1].pop(item))
        return snew
    
    def P(e, enew, T):
        if enew < e: return 1
        return math.exp((e - enew) / T)
    
    def temp(r):
        return (1-r)*100
    
    s = s0()
    e = E(s)
    sbest = s
    ebest = e
    k = 0
    while k < kmax and e > emax:
        snew = neighbour(s)
        enew = E(snew)
        if enew < ebest:
            sbest = snew; ebest = enew
        if P(e, enew, temp(k/kmax)) > random.random():
            s = snew; e = enew
        k += 1
    
    print sbest
    

    Update: After playing around with Branch’n’Bound, I now believe this method to be superior, as it gives perfect results for the N=30, M=6 case within a second. However I guess you could play around with the simulated annealing approach just as much.

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

Sidebar

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.