Java Array Dot Product: IntStream and Integer Bounds
The dot product of two arrays is the sum of products at matching indices. Adding the elements first, or multiplying the two array sums, computes something different.
The exercise supplies equal-length integer arrays of length 1–1,000 with elements from -1,000 to 1,000. Within those bounds, an int result is sufficient.
Processing flow
Map each shared index to a product and sum those products. The exercise bounds fit int.
Sum the products by index
Save this complete class as Solution.java. It was compiled for Java 11 and assumes the equal-length input contract.
import java.util.stream.IntStream;
public class Solution {
public int solution(int[] a, int[] b) {
return IntStream.range(0, a.length)
.map(i -> a[i] * b[i])
.sum();
}
}
IntStream.range(0, a.length) excludes the end index. Map each index to a[i] * b[i], then combine the products with sum(). The primitive index stream does not require building a boxed Integer list.
For [1, 2, 3, 4] and [-3, -1, 0, 2], the products are -3, -2, 0, 8, giving 3. Preserve negative signs: taking absolute values changes the dot product.
When int is sufficient
Each product has absolute value at most 1,000,000, and there are at most 1,000 terms. The absolute sum is therefore bounded by 1,000,000,000, within Java’s int range. Boundary checks also cover the largest positive and negative sums across 1,000 terms.
For larger inputs, changing only the accumulator to long may be insufficient. (long) (a[i] * b[i]) converts after int multiplication has already overflowed. Promote before multiplication with (long) a[i] * b[i], and design the sum and return type as long too. The submitted method here retains the exercise’s int contract.
Time is O(n) and additional storage is O(1). This is not a general vector library with validation for unequal lengths, null arrays, or out-of-range elements.
For converting fixed English digit names into an integer, see digit words and numeric strings.
Discussion