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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:09:30+00:00 2026-05-22T21:09:30+00:00

I am trying to practice the interview question in the title For those who

  • 0

I am trying to practice the interview question in the title

For those who directly want to know my question , jump to “Smaller version of my Question” .

For more context , read on.

=>For N = 2,
We can simply use a Map.

=>For N= 3,
there is a n^2 solution : Finding three elements in an array whose sum is closest to a given number
[Check the accepted answer, it actually give you a solution for sum to 0 , rather than upto as in title of the link]

I guess using the above link, we can have 3 pointers for N=4 and get a n^3 solution.

Anyways, the point I am making that eventually all these solutions are not going to scale as N grows. So I am looking for a general purpose solution

I think this can be derived from a subset sum problem Subset sum problem . However the solution [dynamic programming version] to this problem returns a boolean. I am looking to get the actual subset.
The boolean solution itself isn’t completely natural to derive [in the sense you cant ‘derive’ the answer easily unless you have read it.. I had to write down an example to understand it]. Anyways, I am looking for a way to modify it to give me a subset rather than just a boolean.

Smaller version of my Question:
Modify this to return the actual subset than just the boolean

Let S[i] = true if we can make sum i and false otherwise.

S[0] = true // we can always make sum 0: just don't choose any number
S[i] = false for all i != 0
for each number i in your input
    for s = MaxSum downto i
        if ( S[s - i] == true )
            S[s] = true; // if we can make the sum s - i, we can also make the sum s by adding i to the sum s - i.
  • 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-22T21:09:31+00:00Added an answer on May 22, 2026 at 9:09 pm

    Just make S[s] contain the list of numbers that make that sum when it is possible:

    Let S[i] = the list of numbers that make up that sum and false (or null, something distinct from an empty list) otherwise.
    
    S[0] = empty list // we can always make sum 0: just don't choose any number
    S[i] = null for all i != 0
    for each number i in your input
        for s = MaxSum downto i
            if ( S[s - i] != null )
                S[s] = S[s-i] with i added; // if we can make the sum s - i, we can also make the sum s by adding i to the sum s - i.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to complete a practice question from a book on generics but the
I am trying to figure out what the common practice is. I know there
I've got a 'best practice' question about using PDO. I'm trying to finally get
Context: I am trying to practice BDD in a Ruby on Rails environment using
I am trying to practice a little bit with cpp and want to make
Recently I was trying some practice programs in python and I came across this
I'm trying to practice writing better code, so I wanted to validate my input
I'm trying to practice my F# by writing small console scripts in F# in
I am fairly new to vim. I am trying to practice (been reading a
I'm using Eclipse with C++ plugins on my macbook, trying some practice projects to

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.