Find Receiver After K Seconds

mail@pastecode.io avatar
unknown
c_cpp
a year ago
594 B
6
Indexable
Never
int getReceiver(vector<int> &receivers, int seconds){

    vector<int> actualPlayers;
    map<int, int> players;
    int player = 1;                                 // currrent player

    while(!players[player]){                        // ensures no looping occurs
        players[player]++;                          // register player
        actualPlayers.push_back(player);            // order of players receiving the ball first
        int nextPlayer = receivers[player-1]; 
        player = nextPlayer;
    }

    return actualPlayers[seconds%actualPlayers.size()];
    
}