Home Software Engineering The way to Fold an Array in Java

## The problem#

On this problem, you must write a technique that folds a given array of integers by the center x-times.

An instance says greater than a thousand phrases:

``````Fold 1-times:
[1,2,3,4,5] -> [6,6,3]

A bit of visualization (NOT for the algorithm however for the thought 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 rely of numbers is odd, the center quantity will keep. In any other case, the fold-point is between the middle-numbers, so all numbers can be added in a manner.

The array will all the time include numbers and can by no means be null. The parameter runs will all the time be a optimistic integer higher than 0 and say what number of runs of folding your technique has to do.

If an array with one component is folded, it stays as the identical array.

The enter array shouldn’t be modified!

## The answer in Java code#

Choice 1:

``````public class Resolution {
public static int[] foldArray(int[] a, int r) r==1 ? f : foldArray(f,--r);

}
``````

Choice 2:

``````import java.util.Arrays;
import java.util.stream.IntStream;

public class Resolution {
public static int[] foldArray(int[] array, int runs) {
remaining int[] outcome = Arrays.copyOfRange(array, 0, Math.spherical(array.size / 2.0f));
IntStream.vary(0, array.size / 2)
.forEach(i -> outcome[i] = array[i] + array[array.length - 1 - i]);
return runs > 1 ? foldArray(outcome, --runs) : outcome;
}
}
``````

Choice 3:

``````import java.util.*;

public class Resolution {
public static int[] foldArray(int[] array, int runs) {
int[] arr = Arrays.copyOf(array, array.size);
int certain = arr.size;
for (int ok = 0; ok < runs; ok++) {
for (int i = 0; i < certain / 2; i++) {
arr[i] += arr[bound - 1 - i];
}
certain = (certain + 1) / 2;
}
return Arrays.copyOf(arr, certain);
}
}
``````

## Take a look at instances to validate our answer#

``````import org.junit.Take a look at;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
import java.util.Arrays;

public class SolutionTests {
@Take a look at
public void basicTests() {
int[] enter = new int[] { 1, 2, 3, 4, 5 };
int[] anticipated = new int[] { 6, 6, 3 };
assertEquals(Arrays.toString(anticipated), Arrays.toString(Resolution.foldArray(enter, 1)));

anticipated = new int[] { 9, 6 };
assertEquals(Arrays.toString(anticipated), Arrays.toString(Resolution.foldArray(enter, 2)));

anticipated = new int[] { 15 };
assertEquals(Arrays.toString(anticipated), Arrays.toString(Resolution.foldArray(enter, 3)));

enter = new int[] { -9, 9, -8, 8, 66, 23 };
anticipated = new int[] { 14, 75, 0 };
assertEquals(Arrays.toString(anticipated), Arrays.toString(Resolution.foldArray(enter, 1)));
}

@Take a look at
public void extendedTests() {
int[] enter = new int[] { 1, 2, 3, 4, 5, 99, 88, 78, 74, 73 };
assertEquals(Arrays.toString(new int[] { 427 }), Arrays.toString(Resolution.foldArray(enter, 5)));

enter = new int[] { -1, -2, -3, -4, -5, -99, -88, -78, -74, -73 };
assertEquals(Arrays.toString(new int[] { -427 }), Arrays.toString(Resolution.foldArray(enter, 5)));

enter = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
assertEquals(Arrays.toString( new int[] { 0 }), Arrays.toString(Resolution.foldArray(enter, 10)));

enter = new int[] { 2 };
assertEquals(Arrays.toString(enter), Arrays.toString(Resolution.foldArray(enter, 1)));

enter = new int[] { 2 };
assertEquals(Arrays.toString(enter), Arrays.toString(Resolution.foldArray(enter, 20)));
}

interface FoldArrayInterface {
int[] foldArray(int[] array, int runs);
}

@Take a look at
public void randomTests() {

FoldArrayInterface myMethod = new FoldArrayInterface() {
public int[] foldArray(int[] array, int runs) {
int size = (int)Math.ceil(array.size/2.0);
int[] foldedArray = new int[length];

for(int i=0;i<size;i++) {
if(i != array.size - 1 - i) {
foldedArray[i] = array[i] + array[array.length - 1 - i];
} else {
foldedArray[i] = array[i];
}
}

if(runs == 1) {
return foldedArray;
}
return foldArray(foldedArray, runs - 1);
}
};

for(int r=0;r<20;r++) {
int size = (int)(Math.random() * 199 + 1);
int runs = (int)(Math.random() * 19 + 1);
int[] enter = new int[length];
for(int i=0;i<size;i++) {
enter[i] = (int)(Math.random() * 401 - 200);
}

int[] anticipated = myMethod.foldArray(enter, runs);
assertEquals(Arrays.toString(anticipated), Arrays.toString(Resolution.foldArray(enter, runs)));
}
}
}
``````