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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:24:45+00:00 2026-06-06T18:24:45+00:00

I am working on some review material for dynamic programming. I need to come

  • 0

I am working on some review material for dynamic programming. I need to come up with how the subproblems should be divided, work out the base case, and come up with the recursive formula.

Given n positive integers a1,a2,…,an, a number k and a target W, we want to select a subset T with exactly k elements whose sum is closest to W. Each element can be chosen only once. Define a subproblem with 3 parameters (ie, C[x,y,z] = …).

I have only worked with a few dynamic programming examples, and have never worked with one that needs 3 parameters in defining the subproblem. I am really lost here. If anyone can shine some light that would be great.

My best guess for what the subproblem should be is:

C[x,y,z] = x number of elements from {a1,…ay} where the sum is exactly z

But I have no idea if this is correct.

  • 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-06T18:24:46+00:00Added an answer on June 6, 2026 at 6:24 pm

    One way to break this down into three parameters is:

    x: maximum index of item considered for inclusion in subset
    n: number of items in subset
    s: sum of subset
    

    Base case is C[0,0,0] = true, C[0,i > 0,j > 0] = false

    Recursive case is:

    C[i,n+1,s+a_i] = C[i-1,n,s]  // use item i
    C[i,n,s] = C[i-1,n,s] // dont use item i
    

    This uses space O(n^2 * max(a_i)) (can be reduced to O(n*max(a_i)) by discarding C[i,,] as it is used)

    Then just search C[n,i,j] for j near z for the final answer.

    As a loop…

    for (int i = 1; i <= n; i++)
    {
        C[i,n+1,s+a_i] ||= C[i-1,n,s];
        C[i,n,s] ||= C[i-1,n,s];
    }
    

    As recursive function:

    bool C(int maxindex, int size, int sum)
    {
        if (memoize(maxindex, size, sum))
             return cached;
    
        if (maxindex == 0)
            return (size == 0 && sum == 0);
    
        return
             C(maxindex-1, size-1, sum - A[maxindex]) ||  // version using A[maxindex]
             C(maxindex-1, size, sum); // version not using A[maxindex]
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I working with some regular expression matching and I'm trying to figure out how
Currently, working with RC0 Denali, having some issues I am attempting to review data
I need some help with re-working the logic on this php code. What I'd
Basically I am working on a review form in which users fill out items
I'm working some code that inserts csv rows into an SQLite database using Python.
I've been re-working some code due to a memory leak. The code is part
Working on some tweaks for a build script, I noticed that the output from
Working on some code for a machine problem; we've just started working with pointers,
Whilst working on some generally horrible Javascript code this morning, I came across the
I have some working code with a crutch to add BOM marker to a

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.