From 2b0a3b03a8d13ac06532c3a803ec08286eaa8bb6 Mon Sep 17 00:00:00 2001 From: aniders <33834501+aniders@users.noreply.github.com> Date: Wed, 14 Feb 2018 23:45:39 +0530 Subject: [PATCH] Update Knapsack.java Changed OR condition to &&, OR may possibly result in ArrayIndexOutOfBoundException , (as it does for following input capacity : 60, weights: [10, 20, 33], values: [10, 3, 30]) --- src/com/jwetherell/algorithms/mathematics/Knapsack.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/jwetherell/algorithms/mathematics/Knapsack.java b/src/com/jwetherell/algorithms/mathematics/Knapsack.java index 69f848c2..09079ce9 100644 --- a/src/com/jwetherell/algorithms/mathematics/Knapsack.java +++ b/src/com/jwetherell/algorithms/mathematics/Knapsack.java @@ -40,7 +40,7 @@ public static final int[] zeroOneKnapsack(int[] values, int[] weights, int capac final List list = new ArrayList(); int i = height - 1; int j = width - 1; - while (i != 0 || j != 0) { + while (i != 0 && j != 0) { int current = output[i][j]; int above = output[i - 1][j]; if (current == above) {