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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:19:50+00:00 2026-05-26T05:19:50+00:00

This is my task The Knapsack Problem is a classic in computer science. In

  • 0

This is my task

The Knapsack Problem is a classic in computer science. In its simplest
form it involves trying to fit items of different weights into a
knapsack so that the knapsack ends up with a specified total weight.
You don’t need to fit in all the items. For example, suppose you want
your knapsack to weigh exactly 20 pounds, and you have five items,
with weights of 11, 8, 7, 6, and 5 pounds. For small numbers of items,
humans are pretty good at solving this problem by inspection. So you
can probably figure out that only the 8, 7, and 5 combination of items
adds up to 20.

I really don’t know where to begin writing this algorithm. I understand recursion when applied to factorials and triangle numbers. However I’m lost right now.

  • 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-26T05:19:51+00:00Added an answer on May 26, 2026 at 5:19 am

    What did you try?

    The idea, given the problem you stated (which specifies we must use recursion) is simple: for each item that you can take, see if it’s better to take it or not. So there are only two possible path:

    1. you take the item
    2. you don’t take it

    When you take the item, you remove it from your list and you decrease the capacity by the weight of the item.

    When you don’t take the item, you remove if from you list but you do not decrease the capacity.

    Sometimes it helps to print what the recursive calls may look like. In this case, it could look like this:

    Calling 11 8 7 6 5  with cap: 20
     +Calling 8 7 6 5  with cap: 20
     |  Calling 7 6 5  with cap: 20
     |    Calling 6 5  with cap: 20
     |      Calling 5  with cap: 20
     |      Result: 5
     |      Calling 5  with cap: 14
     |      Result: 5
     |    Result: 11
     |    Calling 6 5  with cap: 13
     |      Calling 5  with cap: 13
     |      Result: 5
     |      Calling 5  with cap: 7
     |      Result: 5
     |    Result: 11
     |  Result: 18
     |  Calling 7 6 5  with cap: 12
     |    Calling 6 5  with cap: 12
     |      Calling 5  with cap: 12
     |      Result: 5
     |      Calling 5  with cap: 6
     |      Result: 5
     |    Result: 11
     |    Calling 6 5  with cap: 5
     |      Calling 5  with cap: 5
     |      Result: 5
     |    Result: 5
     |  Result: 12
     +Result: 20
      Calling 8 7 6 5  with cap: 9
        Calling 7 6 5  with cap: 9
          Calling 6 5  with cap: 9
            Calling 5  with cap: 9
            Result: 5
            Calling 5  with cap: 3
            Result: 0
          Result: 6
          Calling 6 5  with cap: 2
            Calling 5  with cap: 2
            Result: 0
          Result: 0
        Result: 7
        Calling 7 6 5  with cap: 1
          Calling 6 5  with cap: 1
            Calling 5  with cap: 1
            Result: 0
          Result: 0
        Result: 0
      Result: 8
    Result: 20
    

    I did on purpose show the call to [8 7 6 5] with a capacity of 20, which gives a result of 20 (8 + 7 + 5).

    Note that [8 7 6 5] is called twice: once with a capacity of 20 (because we didn’t take 11) and once with a capacity of 9 (because with did take 11).

    So the path to the solution:

    11 not taken, calling [8 7 6 5] with a capacity of 20

    8 taken, calling [7 6 5] with a capacity of 12 (20 – 8)

    7 taken, calling [6 5] with a capacity of 5 (12 – 7)

    6 not taken, calling [5] with a capacity of 5

    5 taken, we’re at zero.

    The actual method in Java can fit in very few lines of code.

    Since this is obviously homework, I’ll just help you with a skeleton:

    private int ukp( final int[] ar, final int cap ) {
        if ( ar.length == 1 ) {
            return ar[0] <= cap ? ar[0] : 0;
        } else {
            final int[] nar = new int[ar.length-1];
            System.arraycopy(ar, 1, nar, 0, nar.length);
            fint int item = ar[0];
            if ( item < cap ) {
                final int left = ...  // fill me: we're not taking the item
                final int took = ...  // fill me: we're taking the item
                return Math.max(took,left);
            } else {
                return ... // fill me: we're not taking the item
            }
        }
    }
    

    I did copy the array to a new array, which is less efficient (but anyway recursion is not the way to go here if you seek performance), but more “functional”.

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

Sidebar

Related Questions

Hello I am solving this task Problem28 , I think that its a easy
I'm trying to solve next task: Given a set of items, each with a
I'm trying to wrap my head around this task and wondering if there is
I am stuck trying to perform this task and while trying I can't help
I am trying to do this task from the past 12 hour, but still
I have an algorithmic problem which can be reduced to this task: Suppose we
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
I was once given this task to do in an RDBMS: Given tables customer,
I have this task for the project with 4 nested subprojects using Maven: For
Please explain what this task is about? Create a generic linked list class that

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.