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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:26:15+00:00 2026-06-13T17:26:15+00:00

My question is about a CodeFu practice problem (2012 round 2 problem 3). It

  • 0

My question is about a CodeFu practice problem (2012 round 2 problem 3). It basically comes down to splitting an array of integers in two (almost) equal halves and returning the smallest possible difference between the two. I have included the problem description below. As noted in the comments this can be described as a balanced partition problem, which is a problem in the realm of dynamic programming.

Now similar problems have been discussed a lot, but I was unable find an efficient solution for this particular one. The problem is of course that the number of possible combinations to traverse soon grows too large for a brute force search (at least when using recursion). I have a recursive solution that works fine for all but the largest problem sets. I tried to add some optimizations that stop the recursion early, but the performance is still too slow to solve some arrays of the maximum length (30) within the 5 second maximum allowed by CodeFu. Any suggestions for how to improve or rewrite the code would be very welcome. I would also love to know if it might help to make the iterative version.

Update: on this fine site there is a theoretical discussion of the balanced partition problem, which gives a good idea of how to go about and solve this in a dynamic way. That is really what I am after, but I do not know how to put the theory into practice exactly. The movie mentions that the elements in the two subcollections can be found “using the old trick of back pointers”, but I don’t see how.

Problem

You and your friend have a number of coins with various amounts. You
need to split the coins in two groups so that the difference between
those groups in minimal.

E.g. Coins of sizes 1,1,1,3,5,10,18 can be split as: 1,1,1,3,5 and
10,18 1,1,1,3,5,10 and 18 or 1,1,3,5,10 and 1,18 The third
combination is favorable as in that case the difference between the
groups is only 1. Constraints: coins will have between 2 and 30
elements inclusive each element of coins will be between 1 and
100000 inclusive

Return value: Minimal difference possible when coins are split into
two groups

NOTE: the CodeFu rules state that the execution time on CodeFu’s server may be no more than 5 seconds.

Main Code

Arrays.sort(coins);

lower = Arrays.copyOfRange(coins, 0,coins.length-1);
//(after sorting) put the largest element in upper
upper = Arrays.copyOfRange(coins, coins.length-1,coins.length);            

smallestDifference = Math.abs(arraySum(upper) - arraySum(lower));
return findSmallestDifference(lower, upper, arraySum(lower), arraySum(upper), smallestDifference);

Recursion Code

private int findSmallestDifference (int[] lower, int[] upper, int lowerSum, int upperSum, int smallestDifference) {
    int[] newUpper = null, newLower = null;
    int currentDifference = Math.abs(upperSum-lowerSum);
    if (currentDifference < smallestDifference) {
        smallestDifference = currentDifference;
    } 
    if (lowerSum < upperSum || lower.length < upper.length || lower[0] > currentDifference 
            || lower[lower.length-1] > currentDifference 
            || lower[lower.length-1] < upper[0]/lower.length) {
        return smallestDifference;
    }
    for (int i = lower.length-1; i >= 0 && smallestDifference > 0; i--) {           
       newUpper = addElement(upper, lower[i]);
       newLower = removeElementAt(lower, i);
       smallestDifference = findSmallestDifference(newLower, newUpper, 
               lowerSum - lower[i], upperSum + lower [i], smallestDifference);
    }
    return smallestDifference;
}

Data Set

Here is an example of a set that takes too long to solve.

{100000,60000,60000,60000,60000,60000,60000,60000,60000,
60000,60000,60000,60000,60000,60000,60000,60000,60000,
60000,60000,60000,60000,60000,60000,60000,60000,60000,
60000,60000,60000}

If you would like the entire source code, I have put it on Ideone.

  • 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-13T17:26:16+00:00Added an answer on June 13, 2026 at 5:26 pm

    Say N is the sum of all coins. We need to find a subset of coins, where the sum of its coins is closest to N/2. Let’s calculate all possible sums and choose the best one. In worst case we may expect 2^30 possible sums, but this may not happen, because the largest possible sum is 100K*30, that is 3M – much less than 2^30 which would be about 1G. So an array of 3M ints or 3M bits should be sufficient to hold all possible sums.

    So we have array a and a[m] == 1 if and only if m is a possible sum.

    We start from zeroed array and have a[0]=1, because the sum 0 is possible (one has no coins).

    for (every coin)
      for (int j=0; j<=3000000; j++)
        if (a[j] != 0)
          // j is a possible sum so far
          new_possible_sum = j + this_coin
          a[new_possible_sum] = 1
    

    When you finish in 30 * 3M steps you will know all possible sums. Find the number m that is closest to N/2. Your result is abs(N-m - m). I hope I fit in time and memory bounds.

    Edit: A correction is needed and 2 optimizations:

    1. Walk the array in descending order. Otherwise a dollar coin would overwrite the whole array in one go.
    2. Limit the size of the array to N+1 (including 0), to solve smaller coin sets faster.
    3. Since we almost always get 2 identical results: m and N-m, reduce the array size to N/2. Add bound check for new_possible_sum. Throw away greater possible sums.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple question about best-practice. I'm using Kohana... is it okay to use helpers in
Question about OO design. Suppose I have a base object vehicle. And two descendants:
Quick question about database design. If I have two databases: User_DB and Group_DB, and
( This question about refactoring F# code got me one down vote, but also
Question about subclassing in matlab, under the new class system. I've got class A
Question about GridView sorting in VB.NET: I have a GridView with AutoGenerateColumns = True
Question about logics here: What's the most elegant way to make the menu appear/disappear
Question about assignments and variables (* For example *) SP = SparseArray[{},5] or SP
one question about PHP Development. I am building up some Form Fields for Calculations.
eternal question about twoForms: frm02 frm02 = new frm02(); frm02.Text = Objects; ds02 =

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.