Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function findTheWinner(n: number, k: number): number {
- // [1, 2, ...., n]
- const friends = [...new Array(n)].map((_, ix) => ix + 1);
- let target = -1; // k=1 needs to pick the first friend, so we start before them
- for(var i=n; i > 1; i--) {
- target = (target + k) % friends.length; // increment and wrap
- friends.splice(target, 1); // remove the target
- target -= 1; // k=1 needs to pick the friend who just filled in the spot we vacated
- }
- return friends[0];
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement