and why elements is 149998, doesn't the compiler converts these to long?
It does, but only after the arithmetic. So you've got int*int + int, which is an int. It will then cast it to a long. However, at this point you've got an integer overflow (i.e. it went beyond the bounds of an integer)
you need to convert the numbers of the right to a long first
long elements = 2L * Integer.MAX_VALUE + 150000;
or
long elements = 2 * (long)Integer.MAX_VALUE + 150000;
B