#1535
Medium Algorithms Find the winner of an array game
Array Simulation
56.8% acceptance
Feb 25, 2026
1622
88
Given an integer array arr of distinct integers and an integer k.
A game will be played between the first two elements of the array (i.e. arr[0] and arr[1]). In each round of the game, we compare arr[0] with arr[1], the larger integer wins and remains at position 0, and the smaller integer moves to the end of the array. The game ends when an integer wins k consecutive rounds.
Return the integer which will win the game.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn get_winner(arr: Vec<i32>, k: i32) -> i32 {
let k = k as usize;
let mut current = arr[0];
let mut wins = 0;
for &x in &arr[1..] {
if current > x {
wins += 1;
} else {
current = x;
wins = 1;
}
if wins >= k { return current; }
}
current
}
}