2

Here I have 'e' which is an array of arrays.

e[i].val() returns a long value (positive or negative).

My goal is to add up all these values and return the mean value.

The problem is that the total could overflow or underflow causing incorrect calculation.

If the total does indeed reach Long.MAX_VALUE or Long.MIN_VALUE at the end of addition, then the total should remain at Long.MAX_VALUE or Long.MIN_VALUE.

The mean then being Long.MAX_VALUE or Long.MIN_VALUE / e.length.

Can only use basic Java, with the only possible exceptions Integer/Long.MIN/MAX_VALUE.

long total = 0;
for(int i = 0; i < e.length; ++i) {
    total = e[i].val();
}
mean = total/e.length.
Javasaurusrex
  • 123
  • 1
  • 7

0 Answers0