n boxes numbered 0 to n - 1. Box i holds a note with the number next_box[i], the next box to open. All notes carry different numbers, so next_box is a permutation of 0 to n - 1.
Open any box and keep following the notes. Sooner or later you come back to a box you have already opened.
Return the largest number of distinct boxes you can open this way with a well-chosen starting box.next_box=[2, 0, 1, 4, 3] → 3
Starting from box 0 the trail goes 0 → 2 → 1 → 0, visiting three boxes. Boxes 3 and 4 point at each other, a loop of two.
next_box=[0, 1, 2] → 1
Every box points to itself, so each loop is a single box.
next_box=[1, 2, 3, 0] → 4
next_box are distinctnext_box=[2, 0, 1, 4, 3]
3
Run checks the first 3 tests in your browser. Submit checks all 100 tests on the server, hidden ones included.