educative.io

Educative

Why do we have result array of size array length - K + 1?

double[] result = new double[arr.length - K + 1];

Why we have result array with this size ?

Because there are “len - K + 1” sublists with size K in the result array. In the given example len=9 so there will be 9 - 5 + 1 = 5 sublists in the result; E.g.
sublist 1 -> [1, 3, 2, 6, -1]
sublist 2 -> [3, 2, 6, -1, 4]
sublist 3 -> [2, 6, -1, 4, 1]
sublist 4 -> [6, -1, 4, 1, 8]
sublist 5 -> [-1, 4, 1, 8, 2]

An array can contain as many subarrays as there are elements in it minus the length of the subarray

According to bodmas shouldn’t the result of the expression “len - K + 1” → 9-5+1 be 3?