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 8625579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:49:23+00:00 2026-06-12T07:49:23+00:00

So I’m actually going to use this to do some dynamic form layout generation

  • 0

So I’m actually going to use this to do some dynamic form layout generation in ExtJS4, but I think I can simplify the problem down to facilitate discussion without losing anything:

Let’s say I have a list of integers that I want to place inside two containers. When I’m done I want both containers to be “as even as possible”, which in this case means the sums of the integers inside each container are as close to each other as we can get them. In this scenario it doesn’t matter how many integers end up in each container, simply that their sums are close.

Here’s an example of a possible input and a desirable output:

Integers: [1,7,13,3,6,8]
Container A: [13,6] (sum 19) 
Container B: [1,7,3,8] (sum 19)

One naive approach might be to iterate over the integer list once, and put the each integer in the container that currently has the smaller sum:

Integers: [1,7,13,3,6,8]
Container A: [1,13,8] (sum 22)
Container B: [7,3,6] (sum 16)

What algorithm / approach would you guys suggest for tackling this problem? I imagine there are quite a few potential ones. If you find it easier to think in code samples I’ll be implementing the end product in javascript.

Thanks!


EDIT

I like the two approaches presented so far (thanks cheeken and sumudu fernando), both of which give good methods for quickly achieving something close to (or exactly) the ideal answer. For a small enough set of integers I imagine you could just brute force a calculation and definitively say that the particular answer is the best (or tied for best). Any suggestions for doing something like that? In other words, an approach that guarantees an ideal answer at the cost of potentially being slow (or completely infeasible for large problem sets).

  • 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-06-12T07:49:24+00:00Added an answer on June 12, 2026 at 7:49 am
    1. Calculate the sum of all of the elements.
    2. Calculate half of the sum, s.
    3. Apply the Knapsack problem to the number making the target sum s.
    4. Put the solution into one container, and the remaining elements into the other container.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't

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.