## The challenge

In this challenge, you have to write a method that folds a given array of integers by the middle x-times.

An example says more than a thousand words:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 `````` ``````Fold 1-times: [1,2,3,4,5] -> [6,6,3] A little visualization (NOT for the algorithm but for the idea of folding): Step 1 Step 2 Step 3 Step 4 Step5 5/ 5| 5\ 4/ 4| 4\ 1 2 3 4 5 1 2 3/ 1 2 3| 1 2 3\ 6 6 3 ----*---- ----* ----* ----* ----* Fold 2-times: [1,2,3,4,5] -> [9,6] ``````

As you see, if the count of numbers is odd, the middle number will stay. Otherwise, the fold-point is between the middle-numbers, so all numbers would be added in a way.

The array will always contain numbers and will never be null. The parameter runs will always be a positive integer greater than 0 and say how many runs of folding your method has to do.

If an array with one element is folded, it stays as the same array.

The input array should not be modified!

## The solution in Java code

Option 1:

 ``````1 2 3 4 5 6 7 `````` ``````public class Solution { public static int[] foldArray(int[] a, int r) { int[] f = java.util.Arrays.copyOfRange(a,0,(int)Math.ceil(a.length/2.)); for (int i=0; i

Option 2:

 `````` 1 2 3 4 5 6 7 8 9 10 11 `````` ``````import java.util.Arrays; import java.util.stream.IntStream; public class Solution { public static int[] foldArray(int[] array, int runs) { final int[] result = Arrays.copyOfRange(array, 0, Math.round(array.length / 2.0f)); IntStream.range(0, array.length / 2) .forEach(i -> result[i] = array[i] + array[array.length - 1 - i]); return runs > 1 ? foldArray(result, --runs) : result; } } ``````

Option 3:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 `````` ``````import java.util.*; public class Solution { public static int[] foldArray(int[] array, int runs) { int[] arr = Arrays.copyOf(array, array.length); int bound = arr.length; for (int k = 0; k < runs; k++) { for (int i = 0; i < bound / 2; i++) { arr[i] += arr[bound - 1 - i]; } bound = (bound + 1) / 2; } return Arrays.copyOf(arr, bound); } } ``````

## 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 `````` ``````import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; import java.util.Arrays; public class SolutionTests { @Test public void basicTests() { int[] input = new int[] { 1, 2, 3, 4, 5 }; int[] expected = new int[] { 6, 6, 3 }; assertEquals(Arrays.toString(expected), Arrays.toString(Solution.foldArray(input, 1))); expected = new int[] { 9, 6 }; assertEquals(Arrays.toString(expected), Arrays.toString(Solution.foldArray(input, 2))); expected = new int[] { 15 }; assertEquals(Arrays.toString(expected), Arrays.toString(Solution.foldArray(input, 3))); input = new int[] { -9, 9, -8, 8, 66, 23 }; expected = new int[] { 14, 75, 0 }; assertEquals(Arrays.toString(expected), Arrays.toString(Solution.foldArray(input, 1))); } @Test public void extendedTests() { int[] input = new int[] { 1, 2, 3, 4, 5, 99, 88, 78, 74, 73 }; assertEquals(Arrays.toString(new int[] { 427 }), Arrays.toString(Solution.foldArray(input, 5))); input = new int[] { -1, -2, -3, -4, -5, -99, -88, -78, -74, -73 }; assertEquals(Arrays.toString(new int[] { -427 }), Arrays.toString(Solution.foldArray(input, 5))); input = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; assertEquals(Arrays.toString( new int[] { 0 }), Arrays.toString(Solution.foldArray(input, 10))); input = new int[] { 2 }; assertEquals(Arrays.toString(input), Arrays.toString(Solution.foldArray(input, 1))); input = new int[] { 2 }; assertEquals(Arrays.toString(input), Arrays.toString(Solution.foldArray(input, 20))); } interface FoldArrayInterface { int[] foldArray(int[] array, int runs); } @Test public void randomTests() { FoldArrayInterface myMethod = new FoldArrayInterface() { public int[] foldArray(int[] array, int runs) { int length = (int)Math.ceil(array.length/2.0); int[] foldedArray = new int[length]; for(int i=0;i