Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ch.claude_martin.playground;
- import java.util.*;
- public final class SomeClass {
- public static void main(String[] args) {
- int[] a = {2, 1, 18, -5, -5, -15, 0, 0, 1, -1} ;
- int[] pairwiseSum = pairwiseSum(a);
- System.out.println(Arrays.toString(pairwiseSum));
- }
- public static int[] pairwiseSum(int[] a) {
- if (Objects.requireNonNull(a, "array can't be null").length % 2 != 0)
- throw new IllegalArgumentException("length must be even");
- int[] result = new int[a.length / 2];
- for (int i = 0, k = 0; k < result.length;) {
- result[k++] = a[i++] + a[i++];
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement