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;

class Answer {
  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 splitAndAdd(half, n - 1);
    }
    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;
        return splitAndAdd(arr2, n-1);
    
}

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];
        return splitAndAdd(subsequent, n-1);
    }
}

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};
        int[] enter = Answer.splitAndAdd(new int[]{1,2,3,4,5},2);

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

        anticipated = new int[]{15};
        enter = Answer.splitAndAdd(new int[]{1,2,3,4,5},3);

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

        assertEquals(Arrays.toString(anticipated),Arrays.toString(enter));
        
        anticipated = new int[]{183, 125};
        enter = Answer.splitAndAdd(new int[]{32,45,43,23,54,23,54,34},2);

        assertEquals(Arrays.toString(anticipated),Arrays.toString(enter));
        
        anticipated = new int[]{32,45,43,23,54,23,54,34};
        enter = Answer.splitAndAdd(new int[]{32,45,43,23,54,23,54,34},0);

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

        anticipated = new int[]{305, 1195};
        enter = Answer.splitAndAdd(new int[]{3,234,25,345,45,34,234,235,345},3);

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

        anticipated = new int[]{1040, 7712};
        enter = Answer.splitAndAdd(new int[]{3,234,25,345,45,34,234,235,345,34,534,45,645,645,645,4656,45,3},4);

        assertEquals(Arrays.toString(anticipated),Arrays.toString(enter));
        
        anticipated = new int[]{79327};
        enter = Answer.splitAndAdd(new int[]{23,345,345,345,34536,567,568,6,34536,54,7546,456},20);
        
        assertEquals(Arrays.toString(anticipated),Arrays.toString(enter));
    }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments