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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:23:00+00:00 2026-06-17T22:23:00+00:00

I’ve been working through some algorithm programming problems and have a question about one.

  • 0

I’ve been working through some algorithm programming problems and have a question about one. The problem is the same one as the one referenced in this question: USACO: Subsets (Inefficient)

I was able to code some (non-dynamic) solutions that were too slow for high N. Had to cheat a bit and look up some solutions online. It turns out the fast algorithm is trivially simple, but even knowing the answer I still can’t see how to get from the problem to the answer. I can see patterns in the way subsets of equal sums form, but I don’t see the link between those patterns and the algorithmic solution.

The problem (link above) is:

Given a set of consecutive integers from 1 through N (1 <= N <= 39),
how many ways can the set be partitioned into two subsets whose sums
are identical? E.g., {1,2,3} can be partitioned one way: {1,2} {3}.

For larger sets the answer is either 0 (when N*(N+1)/2 is odd) or is given by this simple algorithm:

  arr = array of int with (N*(N+1)/4)+1 elements
  arr[0]=1  // all other elements initialized to 0
  for i = 1 to N
    for j = N*(N+1) / 4 downto i
      add arr[j-i] to arr[j]
  subsetpaircount = arr[N*(N+1)/4] / 2

Again, I can see how the algorithm works, I’ve even inserted print statements so I can “watch” how it works. I just can’t see how the operation of the algorithm links up to the pattern of different ways the two-set partitions are generated.

A response in the linked question may be relevant, but I also can’t connect up how it works: “This is the same thing as finding the coefficient x^0 term in the polynomial (x^1+1/x)(x^2+1/x^2)…(x^n+1/x^n). . . .”

Can anyone clarify the connection for me, or point me to some reference materials that explain this specific problem? 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-06-17T22:23:01+00:00Added an answer on June 17, 2026 at 10:23 pm

    If the set S = {1,...,N} is partitioned into two subsets with equal sum, that sum must be half of the sum of S; the sum of S is N(N+1)/2 so the sum of each subset in the partition must be N(N+1)/4. It must also be an integer, hence N(N+1)/2 must be even.

    So the question reduces to finding the number of subsets of S whose sum is N(N+1)/4. The total number of partitions will be exactly half this number, since each partition contains two such subsets, and no two partitions share a subset.

    That much should be obvious.

    Now, let’s list the subsets of S which sum to k, for any k and any set S. Any such subset must have a greatest value, which must be an element of S. The greatest value must either be the greatest element of S, or it must be less than the greatest element of S. These two groups of subsets are distinct, so we can enumerate them separately. Let’s call the greatest element of S Smax.

    The second group is simple: it’s simply the subsets of S - { Smax } which sum to k. We can find those by recursively calling the subset lister. But the first group is almost as simple: each set in the group includes Smax and the rest of its elements are some set in S - { Smax } which adds up to k - Smax, which again we can list recursively. To complete the recursion, we note that if S is the empty set, then if k = 0, there is precisely one qualify subset (the empty set itself), and if k is not 0, then there are no qualifying subsets. Each recursion removes one element from S, so the termination condition must eventually be reached.

    It should be clear that the subsets of S which will be used by the above recursive function are just the numbers from 1 to Smax, so we can get rid of S altogether, and write the recursion as follows:

    Subsets(min, max, k) =
      Subsets(min, max - 1, k)
      ⋃ { {max, P} | P ∈ Subsets(min, max - 1, k - max) }

    But we only want the count of the number of partitions, so we can simplify that a bit:

    Count_Subsets(min, max, k) =
      Count_Subsets(min, max - 1, k)
      + Count_Subsets(min, max - 1, k - max)

    We need to complete the recursion by adding the end condition:

    If min > max, Count_Subsets(min, max, k) = 1 if k = 0; otherwise 0

    (In fact, it’s simple to show that the recursion implies that the value will be 1 when k decrements to 0, and 0 if k is less than 0, so we can make the termination condition happen a lot earlier.)

    That gives us the simple recursion for the count. But since it calls itself twice, it would be better to work backwards (“dynamic programming”). We need to compute Count_Subsets(1, N, N*(N+1)/4), which will require us to compute values of Count_Subsets(1, max, k) for all values of max from 1 to N, and from all values of k from 0 to N*(N+1)/4. We do that by starting with max = 0, and working up until we reach min = N. And that’s precisely what your algorithm does; i is max, and the array is the set of values for k from 0 to N(N+1)/4.

    By the way, as should be apparent from the above description, a[j] should be incremented by a[j - i], not by a[j - 1]

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

Sidebar

Related Questions

I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have been unable to fix a problem with Java Unicode and encoding. The
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
This could be a duplicate question, but I have no idea what search terms
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.