## The challenge

### Get the averages of these numbers

Write a method, that gets an array of integer-numbers and return an array of the averages of each integer-number and his follower, if there is one.

Example:

 ``````1 2 `````` ``````Input: [ 1, 3, 5, 1, -10] Output: [ 2, 4, 3, -4.5] ``````

If the array has 0 or 1 values or is null, your method should return an empty array.

## The solution in Java code

Option 1:

 ``````1 2 3 4 5 6 7 `````` ``````public class Solution { public static double[] averages(final int[] numbers) { final double res[] = new double[(numbers == null || numbers.length == 0) ? 0 : numbers.length - 1]; for (int i = 0; i < res.length; i++) res[i] = (numbers[i]+numbers[i+1]) / 2.0; return res; } } ``````

Option 2:

 ``````1 2 3 4 5 6 7 8 `````` ``````public class Solution { public static double[] averages(int[] numbers) { if (numbers == null || numbers.length < 2) return new double[] {}; double [] ans = new double [numbers.length - 1]; for (int i = 0; i < ans.length; i++) ans[i] = (numbers[i] + numbers[i + 1]) / 2.0; return ans; } } ``````

Option 3:

 `````` 1 2 3 4 5 6 7 8 9 10 `````` ``````import java.util.stream.IntStream; public class Solution { public static double[] averages(int[] numbers) { if (numbers == null || numbers.length < 2) return new double[0]; return IntStream.range(1, numbers.length) .mapToDouble(i -> (numbers[i - 1] + numbers[i]) / 2.0) .toArray(); } } ``````

## Test cases to validate our solution

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 `````` ``````import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertArrayEquals; import org.junit.runners.JUnit4; import java.util.Arrays; public class SolutionTest { @Test public void basicTests() { assertEquals(Arrays.toString(new double[] { 2, 2, 2, 2 }), Arrays.toString(Solution.averages(new int[] { 2, 2, 2, 2, 2 }))); assertEquals(Arrays.toString(new double[] { 0, 0, 0, 0 }), Arrays.toString(Solution.averages(new int[] { 2, -2, 2, -2, 2 }))); assertEquals(Arrays.toString(new double[] { 2, 4, 3, -4.5 }), Arrays.toString(Solution.averages(new int[] { 1, 3, 5, 1, -10 }))); } @Test public void nullEmptyTests() { assertEquals("input 'null' should return an empty array", 0, Solution.averages(null).length); assertEquals("Empty array as input should return an empty array", 0, Solution.averages(new int[0]).length); assertEquals("Array with only one value as input should return an empty array", 0, Solution.averages(new int[] { 2 }).length); } @Test public void randomTests() { for(int r = 0; r < 100; r++) { int len = (int)Math.floor(Math.random() * 20); int[] numbers = new int[len]; for(int k = 0; k < len; k++) { numbers[k] = (int)Math.floor(Math.random() * 41) - 20; } double[] expected = new double[0]; if(numbers.length > 1) { expected = new double[numbers.length - 1]; for(int i=0;i