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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:21:27+00:00 2026-06-03T21:21:27+00:00

How would you use dynamic programming to find the list of positive integers in

  • 0

How would you use dynamic programming to find the list of positive integers in an array whose sum is closest to but not equal to some positive integer K?

I’m a little stuck thinking about this.

  • 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-03T21:21:29+00:00Added an answer on June 3, 2026 at 9:21 pm

    The usual phrasing for this is that you’re looking for the value closest to, but not exceeding K. If you mean “less than K”, it just means that your value of K is one greater than the usual. If you truly mean just “not equal to K”, then you’d basically run through the algorithm twice, once finding the largest sum less than K, then again finding the smallest sum greater than K, then picking the one of those whose absolute difference from K is the smallest.

    For the moment I’m going to assume you really mean the largest sum less than or equal to K, since that’s the most common formulation, and the other possibilities don’t really have much affect on the algorithm.

    The basic idea is fairly simple, though it (at least potentially) uses a lot of storage. We build a table with K+1 columns and N+1 rows (where N = number of inputs). We initialize the first row in the table to 0’s.

    Then we start walking through the table, and building the best value we can for each possible maximum value up to the real maximum, going row by row so we start with only a single input, then two possible inputs, then three, and so on. At each spot in the table, there are only two possibilities for the best value: the previous best value that doesn’t use the current input, or else the current input plus the previous best value for the maximum minus the current input (and since we compute the table values in order, we’ll always already have that value).

    We also usually want to keep track of which items were actually used to produce the result. To do that, we set a Boolean for a given spot in the table to true if and only if we compute a value for that spot in the table using the new input for that row (rather than just copying the previous row’s best value). The best result is in the bottom, right-hand corner, so we start there, and walk backward through the table one row at a time. When we get to a row where the Boolean for that column was set to true, we know we found an input that was used. We print out that item, and then subtract that from the total to get the next column to the left where we’ll find the next input that was used to produce this output.

    Here’s an implementation that’s technically in C++, but written primarily in a C-like style to make each step as explicit as possible.

    #include <iostream>
    #include <functional>
    
    #define elements(array) (sizeof(array)/sizeof(array[0]))
    
    int main() {
    
        // Since we're assuming subscripts from 1..N, I've inserted a dummy value
        // for v[0].
        int v[] = {0, 7, 15, 2, 1};
    
        // For the moment I'm assuming a maximum <= MAX.
        const int MAX = 17;
    
        // ... but if you want to specify K as the question implies, where sum<K, 
        // you can get rid of MAX and just specify K directly:
        const int K = MAX + 1;
    
        const int rows = elements(v);
    
        int table[rows][K] = {0};
        bool used[rows][K] = {false};
    
        for (int i=1; i<rows; i++)
            for (int c = 0; c<K; c++) {
                int prev_val = table[i-1][c];
                int new_val;
    
                // we compute new_val inside the if statement so we won't 
                // accidentally try to use a negative column from the table if v[i]>c
                if (v[i] <= c && (new_val=v[i]+table[i-1][c-v[i]]) > prev_val) {
                    table[i][c] = new_val;
                    used[i][c] = true;
                }
                else
                    table[i][c] = prev_val;
            }
    
        std::cout << "Result: " << table[rows-1][MAX] << "\n";
        std::cout << "Used items where:\n";
        int column = MAX;
        for (int i=rows; i>-1; i--)
            if (used[i][column]) {
                std::cout << "\tv[" << i << "] = " << v[i] << "\n";
                column -= v[i];
            }
    
        return 0;
    }
    

    There are a couple of things you’d normally optimize in this (that I haven’t for the sake of readability). First, if you reach an optimum sum, you can stop searching, so in this case we could actually break out of the loop before considering the final input of 1 at all (since 15 and 2 give the desired result of 17).

    Second, in the table itself we only really use two rows at any given time: one current row and one previous row. The rows before that (in the main table) are never used again (i.e., to compute row[n] we need the values from row[n-1], but not row[n-2], row[n-3], … row[0]. To reduce storage, we can make the main table be only two rows, and we swap between the first and second rows. A very C-like trick to do that would be to use only the least significant bit of the row number, so you’d replace table[i] and table[i-1] with table[i&1] and table[(i-1)&1] respectively (but only for the main table — not when addressing the used table.

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

Sidebar

Related Questions

I want to use dynamic object recognition (descriptive programming) to find the first button
I was practicing the dynamic programming problem on SPOJ. But I have no idea
Would you use dynamic cast in a frequently run method? Does it have a
I am planning to use dynamic keyword for my new project. But before stepping
Consider a dynamic programming problem that asks how many distinct subsequences (not necessarily contiguous)
I would like to use dynamic $_POSTs, I don't know if I am using
I'm new to programming but have managed to build a rather complex dynamic site
Using .NET-4.0, how would I use Dynamic to accomplish the following without using reflection?
I would like to use dynamic properties, which look like this: public class CustomProperty
I would use a multi dimensional gaussian modell for regression. Rasmussen has a book

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.