← Writing

Sum Signed Values in Java with IntStream

When magnitudes and signs arrive separately, apply the sign at each matching index before summing. signs[i] means positive when true and negative when false, so one conditional expression reconstructs each value.

The exercise guarantees equal array lengths from 1 to 1,000 and magnitudes from 1 to 1,000. Sorting the signs separately would break their association with the original values; process the arrays in their given order.

Processing flow

Read the same index in both arrays, apply its sign, then sum the signed values.

Read the same index in both arrays, apply its sign, then sum the signed values.

Read matching indices together

Save this complete class as Solution.java. It was compiled for Java 11 and executed on JDK 17.

import java.util.stream.IntStream;

public class Solution {
    public int solution(int[] absolutes, boolean[] signs) {
        return IntStream.range(0, absolutes.length)
                .map(i -> signs[i] ? absolutes[i] : -absolutes[i])
                .sum();
    }
}

IntStream.range includes index 0 and excludes absolutes.length. map applies the sign to each magnitude, and sum adds the resulting values. The method neither modifies the input nor creates a separate array of signed values.

Magnitude Sign Actual value
4 true 4
7 false -7
12 true 12

These values sum to 9. Input [1,2,3] with [false,false,true] cancels to 0. Counting positive versus negative flags alone is insufficient: each term’s magnitude matters too.

Integer bounds and contract

There are at most 1,000 terms with magnitude at most 1,000, so the absolute sum is bounded by 1,000,000. Even the all-positive and all-negative boundaries fit int. Time is O(n), with O(1) additional space.

The method assumes the exercise’s valid magnitudes and equal lengths. It does not replace null, length, and value validation for arbitrary application input. Larger bounds require reconsidering both term and accumulator types.

Compare array dot products for another two-array calculation and an x-spaced sequence using long for results beyond int. Browse the Java coding exercise guide for a learning path.

Discussion