Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Not sure how to consider an even number of items into the knapsack. Please expla

ID: 3598410 • Letter: N

Question

Not sure how to consider an even number of items into the knapsack. Please explain how I could get the max sum of values while also considering there must be an even amount of items into the knapsack and then how I can get that set of items.

Problem 5: (5 +2 points) As an input you get positive integer C, the capacity of the knapsack, and an array of n pairs (s, v) of positive integers, where s and v the size and the value of the corresponding item. Design an algorithm that finds a set consisting of even number of items that fit into the knapsack such that the sum of the values is the largest possible. The complexity of the algorithms has to be O(nC).

Explanation / Answer

package com.one;

abstract class Polynomial {

DList<Term> data = null;

public Polynomial() {

data = new DList<>();

}

public final String toString() {

String ans = "";

boolean starting = true;

try {

DNode<Term> n = data.getFirst();

while (n != null) {

if (!starting && n.getData().isPositive())

ans += " +";

starting = false;

ans += " " + n.getData().toString();

n = data.getNext(n);

}

} catch (Exception e) {

if (starting)

return "0";

}

return ans;

}

abstract public Polynomial add(Polynomial p);

abstract public Polynomial subtract(Polynomial p);

abstract public Polynomial multiply(Polynomial p);

abstract public Polynomial divide(Polynomial p) throws Exception;

abstract public Polynomial remainder(Polynomial p) throws Exception;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote