Practice/Amazon/Leetcode 1535. Find the Winner of an Array Game
CodingOptional
You are given an array of distinct integers and an integer k. The array participates in a tournament game with the following rules:
k consecutive roundsReturn the integer that wins k consecutive rounds.
Important observation: If k is greater than or equal to the array length, the maximum element in the array will always win, since it will beat every other element at least once.
k consecutive winsk exceeds the array sizearr are distinctExample 1:
Input: arr = [2, 1, 3, 5, 4, 6, 7], k = 2 Output: 5 Explanation: Round 1: 2 vs 1 → 2 wins (consecutive wins: 1), arr becomes [2, 3, 5, 4, 6, 7, 1] Round 2: 2 vs 3 → 3 wins (consecutive wins: 1), arr becomes [3, 5, 4, 6, 7, 1, 2] Round 3: 3 vs 5 → 5 wins (consecutive wins: 1), arr becomes [5, 4, 6, 7, 1, 2, 3] Round 4: 5 vs 4 → 5 wins (consecutive wins: 2), 5 reaches k=2 wins
Example 2:
Input: arr = [3, 2, 1], k = 10 Output: 3 Explanation: 3 is the maximum element and will beat all others. Since k=10 exceeds the array size, the maximum element 3 will win.
Example 3:
Input: arr = [1, 9, 8, 2, 3], k = 3 Output: 9 Explanation: Round 1: 1 vs 9 → 9 wins (consecutive wins: 1) Round 2: 9 vs 8 → 9 wins (consecutive wins: 2) Round 3: 9 vs 2 → 9 wins (consecutive wins: 3) 9 achieves k=3 consecutive wins.