Skip to content

Commit 1412e33

Browse files
committed
fix(misc): prevent overflow when averaging the two middle values
MedianOfRunningArrayInteger and MedianOfRunningArrayLong both compute (a + b) / 2, which overflows once the two middle values of the stream are large. For an Integer stream holding Integer.MAX_VALUE and Integer.MAX_VALUE - 2 the median came back as -2 instead of 2147483646; the Long version fails the same way near Long.MAX_VALUE. Sum in a long for the Integer case, and in a BigInteger for the Long case since there is no wider primitive. Add regression tests for both types at each end of their range.
1 parent 54d056d commit 1412e33

3 files changed

Lines changed: 43 additions & 2 deletions

File tree

‎src/main/java/com/thealgorithms/misc/MedianOfRunningArrayInteger.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
public final class MedianOfRunningArrayInteger extends MedianOfRunningArray<Integer> {
44
@Override
55
public Integer calculateAverage(final Integer a, final Integer b) {
6-
return (a + b) / 2;
6+
// Widen to long so that summing values near Integer.MAX_VALUE does not overflow.
7+
final long sum = (long) a + b;
8+
return (int) (sum / 2);
79
}
810
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.thealgorithms.misc;
22

3+
import java.math.BigInteger;
4+
35
public final class MedianOfRunningArrayLong extends MedianOfRunningArray<Long> {
46
@Override
57
public Long calculateAverage(final Long a, final Long b) {
6-
return (a + b) / 2L;
8+
// a + b overflows a long near its bounds and there is no wider primitive to widen to.
9+
return BigInteger.valueOf(a).add(BigInteger.valueOf(b)).divide(BigInteger.TWO).longValueExact();
710
}
811
}

‎src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,42 @@ public void testWithLongValues() {
190190
assertEquals(46116860244273878L, stream.getMedian());
191191
}
192192

193+
@Test
194+
public void testWithIntegerValuesNearMaxValue() {
195+
var stream = new MedianOfRunningArrayInteger();
196+
stream.insert(Integer.MAX_VALUE);
197+
assertEquals(Integer.MAX_VALUE, stream.getMedian());
198+
stream.insert(Integer.MAX_VALUE - 2);
199+
assertEquals(Integer.MAX_VALUE - 1, stream.getMedian());
200+
stream.insert(2000000000);
201+
assertEquals(Integer.MAX_VALUE - 2, stream.getMedian());
202+
}
203+
204+
@Test
205+
public void testWithIntegerValuesNearMinValue() {
206+
var stream = new MedianOfRunningArrayInteger();
207+
stream.insert(Integer.MIN_VALUE);
208+
stream.insert(Integer.MIN_VALUE + 2);
209+
assertEquals(Integer.MIN_VALUE + 1, stream.getMedian());
210+
}
211+
212+
@Test
213+
public void testWithLongValuesNearMaxValue() {
214+
var stream = new MedianOfRunningArrayLong();
215+
stream.insert(Long.MAX_VALUE);
216+
assertEquals(Long.MAX_VALUE, stream.getMedian());
217+
stream.insert(Long.MAX_VALUE - 2);
218+
assertEquals(Long.MAX_VALUE - 1, stream.getMedian());
219+
}
220+
221+
@Test
222+
public void testWithLongValuesNearMinValue() {
223+
var stream = new MedianOfRunningArrayLong();
224+
stream.insert(Long.MIN_VALUE);
225+
stream.insert(Long.MIN_VALUE + 2);
226+
assertEquals(Long.MIN_VALUE + 1, stream.getMedian());
227+
}
228+
193229
@Test
194230
public void testWithDoubleValues() {
195231
var stream = new MedianOfRunningArrayDouble();

0 commit comments

Comments
 (0)