You can see the task text here: <https://codility.com/programmers/lessons/8/>

This is my solution:

```
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');

function solution(A) {
var length = A.length,
peaksIndex = [];
for (var i=1;i<length-1;i++){
    if (A[i] > A[i-1] && A[i] > A[i+1]){
        peaksIndex.push(i);
    }
}
var peaksLength = peaksIndex.length;
var maxPeaksLength = Math.min(Math.ceil(Math.sqrt(length)), peaksLength);
var maxFound = 0;
for (var j=maxPeaksLength;j>0;j--){
    //debugger;
    // j is the max number of flags AND distance
    var found = 0;
    var distance = 0;
    for (var k=peaksLength-1;k>=0;k--){
        var prevPeak;
        if (k===0){
            prevPeak = -j;
        } else {
            prevPeak = peaksIndex[k-1];
        }
        distance += peaksIndex[k]-prevPeak;
        if (distance >=j){
            distance = 0;
            found++;
        }
    }
    if (found > maxFound) {
        maxFound = found;
    }
    if (maxFound >= j-1){
        break;
    }
}
return maxFound;
// write your code in JavaScript (Node.js 4.0.0)
}

```

or on Codility: <https://codility.com/demo/results/trainingQJNNVS-U3Y/>

As you can see there, the only incorrect case is

> 
> large\_anti\_slow  
> 
> large test anti slow
> solutions WRONG ANSWER got 317, expected 316
> 
> 
> 1. 0.298 s OK
> 2. 0.216 s WRONG ANSWER, got 317 expected 316
> 
> 
> 

In short, I don't understand what's problematic in my algorithm that it fails only in this single case?