plduhoux
2/22/2018 - 12:09 AM

replaceMiddle

int[] replaceMiddle(int[] arr) {
    if (arr.length % 2 == 0) {
        int mid = arr[arr.length / 2 - 1] + arr[arr.length / 2];
        int res[] = new int[arr.length - 1];
        for (int i = 0; i < arr.length - 1; i++) {
            res[i] = i < arr.length / 2 - 1 ? arr[i] : (i == arr.length / 2 - 1 ? mid : arr[i + 1]);
        }
         return res;  
    } else {
        return arr;
    }
}