Alternate through Forest Positions until 100
Problem statement
Begin at start, record that index, and collect its nonnegative value. Then alternate searching right and left of start. On each side, choose the nearest not-yet-visited index with a positive value, moving farther outward on later visits to that side; zero positions are skipped.
Stop once the collected sum reaches at least 100. If the requested side has no remaining positive value, continue on the other side. If neither side has one, stop. Return visited indices.
Function
forestVisitOrder(forest: int[], start: int) → int[]Examples
Example 1
forest = [0,0,0,20,50,0,0,30]start = 4return = [4,7,3]Nearest positive right then left positions yield [4,7,3].
Example 2
forest = [100,1]start = 0return = [0]The traversal stops immediately when start reaches the target.
Constraints
1 <= forest.length <= 1000000 <= forest[i] <= 1000 <= start < forest.length