 # Min Average Slice – Codility 100% Correct Javascript Solution

The Min Average Slice problem gives more of a mathematical challenge than a coding one.

The trick here is to figure out that you only need to find the minimum average of slices which are 2 or 3 in length. That is because a slice of 4 or larger is basically a sum of slices with the length of 2 or 3 . A composed slice will never have an average sum lower than its components.

Thus the solution is an efficient algorithm with O(N) complexity as shown below (solution in Javascript):

``````
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let B = []
let C = []
let min = 10000
let result = 0;
for (let i = 0; i < A.length-1; i++){
B[i] = (A[i]+A[i+1])/2
if ((B[i]) < min){
min = B[i]
result = i
}
}

let  = 0
for (let i = 0; i < A.length-2; i++){
C[i] = (A[i]+A[i+1]+A[i+2])/3
if ((C[i]) < min){
min = C[i]
result = i
}
}

return(result)

}
``````

Check the solution’s report. If you have a more optimized solution, let me know in the comments below!