Skip to content

Commit 6c31f3f

Browse files
committed
remove unnecessary copy
1 parent 5fa2549 commit 6c31f3f

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/main/java/problems/impl/IntegerProductIgnoringIndex.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ public static int[] calculateProducts(int[] values) {
1414
throw new IllegalArgumentException("Invalid values");
1515
}
1616

17-
int[] products = new int[values.length + 1];
17+
int[] products = new int[values.length];
1818
products[0] = 1;
1919

20-
for (int i = 0; i < values.length; i++) {
20+
for (int i = 0; i < values.length - 1; i++) {
2121
products[i + 1] = products[i] * values[i];
2222
}
2323

24-
int multiplier = products[0];
24+
int multiplier = 1;
2525
for (int j = values.length - 1; j >= 0; j--) {
2626
products[j] *= multiplier;
2727
multiplier *= values[j];
2828
}
2929

30-
return Arrays.copyOfRange(products, 0, products.length - 1);
30+
return products;
3131
}
3232
}

0 commit comments

Comments
 (0)