FastPrepAlternate through Forest Positions until 100

Alternate through Forest Positions until 100

ZipRecruiter logoZipRecruiter● EasyNEW GRADOA
Learn

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 <= 100000
  • 0 <= forest[i] <= 100
  • 0 <= start < forest.length

More ZipRecruiter problems

See ZipRecruiter hiring insights
public int[] forestVisitOrder(int[] forest, int start) {
    // Write your code here.
}
forest[0,0,0,20,50,0,0,30]
start4
expected[4,7,3]
Checking account…