Sunday, April 14, 2024
HomeSoftware EngineeringCut up after which add either side of an array collectively in...

# Cut up after which add either side of an array collectively in Java

## The problem#

You’ll obtain an array as a parameter that incorporates 1 or extra integers and a quantity `n`.

Here’s a little visualization of the method:

• Step 1: Cut up the array in two:`[1, 2, 5, 7, 2, 3, 5, 7, 8] / [1, 2, 5, 7] [2, 3, 5, 7, 8]`
• Step 2: Put the arrays on high of one another: `[1, 2, 5, 7] [2, 3, 5, 7, 8]`
• Step 3: Add them collectively:`[2, 4, 7, 12, 15]`

Repeat the above steps `n` occasions or till there is just one quantity left, after which return the array.

Instances

``````Enter: arr=[4, 2, 5, 3, 2, 5, 7], n=2

Spherical 1
-------
step 1: [4, 2, 5]  [3, 2, 5, 7]

step 2:    [4, 2, 5]
[3, 2, 5, 7]

step 3: [3, 6, 7, 12]

Spherical 2
-------
step 1: [3, 6]  [7, 12]

step 2:  [3,  6]
[7, 12]

step 3: [10, 18]

End result: [10, 18]
``````

## The answer in Java code#

Possibility 1:

``````import static java.util.Arrays.copyOfRange;

static int[] splitAndAdd(int[] numbers, int n) {
if (numbers.size > 1 && n > 0) {
int[] half = copyOfRange(numbers, numbers.size / 2, numbers.size);
for (int i = 0; i < numbers.size / 2; i++) {
half[numbers.length % 2 > 0 ? i + 1 : i] += numbers[i];
}
}
return numbers;
}
}
``````

Possibility 2:

``````public class Answer {
public static int[] splitAndAdd(int[] arr, int n)  arr.size==1) return arr;
int len = arr.size/2;
int[] arr1 = new int[len], arr2 = new int[arr.length-len];
System.arraycopy(arr, 0, arr1, 0, len);
System.arraycopy(arr, len, arr2, 0, arr.length-len);
int i = arr2.size - arr1.size;
for (int a:arr1) arr2[i++] += a;

}
``````

Possibility 3:

``````public class Answer {
public static int[] splitAndAdd(int[] numbers, int n) {
if (n == 0) return numbers;
ultimate int len = numbers.size;
ultimate int subsequent[] = new int[(len+1)/2];
for (int i = 0; i < subsequent.size; i++) subsequent[i] += numbers[len/2+i];
for (int i = len/2-1, j=subsequent.length-1; i >= 0; i--,j--) subsequent[j] += numbers[i];
}
}
``````

## Check circumstances to validate our resolution#

``````import org.junit.Check;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
import java.util.*;

public class TestClass {
@Check
public void splitAndAdd() throws Exception {

int[] anticipated = new int[]{5,10};

assertEquals(Arrays.toString(anticipated), Arrays.toString(enter));

anticipated = new int[]{15};

assertEquals(Arrays.toString(anticipated),Arrays.toString(enter));

anticipated = new int[]{15};

assertEquals(Arrays.toString(anticipated),Arrays.toString(enter));

anticipated = new int[]{183, 125};

assertEquals(Arrays.toString(anticipated),Arrays.toString(enter));

anticipated = new int[]{32,45,43,23,54,23,54,34};

assertEquals(Arrays.toString(anticipated),Arrays.toString(enter));

anticipated = new int[]{305, 1195};

assertEquals(Arrays.toString(anticipated),Arrays.toString(enter));

anticipated = new int[]{1040, 7712};

assertEquals(Arrays.toString(anticipated),Arrays.toString(enter));

anticipated = new int[]{79327};

assertEquals(Arrays.toString(anticipated),Arrays.toString(enter));
}
}
``````
RELATED ARTICLES