Now we know that a [ j . . k ] is a decreasing subarray . Remove non - positive elements from the right side as much as possible . last is needed to keep the last seen element .
let last = k ; while ( k > j && arr [ k ] <= 0 ) k -- ;