Observation: For a particular magic number , among two participant with same and for both of them, the participant with higher will reach there first. If if same for both of them, the one with lower index will reach there first.
As is divisible by we can iterate for all distinct values that can be just once. When we iterate for a specific , let’s consider a specific point within the iteration. We will consider the latest participant i.e the participant with maximum till now. If there is two or more participants with maximum we will prioritize the one with lower index. And, we will eleminate the ones with lesser than . This can be done using a stack. Then we will update the result by counting the number of minimum rounds are needed to reach by the participants with this . We can precalculate it for all possible distinct for all possible magic numbers.
As all the iteration will be done for just once for a distinct number the complexity will be, .