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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:27:29+00:00 2026-05-16T21:27:29+00:00

I have a list of items that has numeric values and I need to

  • 0

I have a list of items that has numeric values and I need to achieve a sum using these items. I need your help to build such an algorithm. Below, there is a sample that describes my problem, written in C#:

int sum = 21;

List<Item> list = new List<Item>();
list.Add(new Item() { Id = Guid.NewGuid(), Value = 3 });
list.Add(new Item() { Id = Guid.NewGuid(), Value = 5 });
list.Add(new Item() { Id = Guid.NewGuid(), Value = 12 });
list.Add(new Item() { Id = Guid.NewGuid(), Value = 3 });
list.Add(new Item() { Id = Guid.NewGuid(), Value = 2 });
list.Add(new Item() { Id = Guid.NewGuid(), Value = 7 });

List<Item> result = // the items in the list that has the defined sum.

Note: I have no constraint on the number of items in the result.

  • 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-16T21:27:29+00:00Added an answer on May 16, 2026 at 9:27 pm

    This is called the Subset sum problem and is considered a hard problem in computer science. Not hard as in hard to do, but hard to do fast — you can easily write an algorithm to do it, but for sizable inputs it will easily take billions of years.

    If you are happy with a slow solution that is only practicable for small inputs, try something like this:

    • Generate all subsets of the input list.

    • For each subset, calculate the sum of the items in that subset.

    • Return the first subset for which the sum matches.

    Here is a method that returns all subsets (actually subsequences because it maintains the order of items, although in your case this makes no difference):

    /// <summary>
    /// Returns all subsequences of the input <see cref="IEnumerable&lt;T&gt;"/>.
    /// </summary>
    /// <param name="source">The sequence of items to generate
    /// subsequences of.</param>
    /// <returns>A collection containing all subsequences of the input
    /// <see cref="IEnumerable&lt;T&gt;"/>.</returns>
    public static IEnumerable<IEnumerable<T>> Subsequences<T>(
            this IEnumerable<T> source)
    {
        if (source == null)
            throw new ArgumentNullException("source");
        // Ensure that the source IEnumerable is evaluated only once
        return subsequences(source.ToArray());
    }
    
    private static IEnumerable<IEnumerable<T>> subsequences<T>(IEnumerable<T> source)
    {
        if (source.Any())
        {
            foreach (var comb in subsequences(source.Skip(1)))
            {
                yield return comb;
                yield return source.Take(1).Concat(comb);
            }
        }
        else
        {
            yield return Enumerable.Empty<T>();
        }
    }
    

    So you can now write something like this…

    var result = list.Subsequences()
                     .FirstOrDefault(ss => ss.Sum(item => item.Value) == sum);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming I have a List<Stuff> listA that has some items in it. I create
I have a list of items that I am displaying in a floated list,
I have a specialized list that holds items of type IThing : public class
I have a webpart that renders random list items (from any list and list
I have a ListCtrl that displays a list of items for the user to
I have an application that shows a list of items. The user can click
I want to have a select-only ComboBox that provides a list of items for
I have a (derived) Menu control, that displays a rather large list of items
For instance, I have a SqlDataSource that loads a list of items. On my
I have two string lists that I'm working with. One that has a list

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.