✅ 100% ✅ O(N) Time O(1) Space ✅Last Winner ✅ Video Explanation
\n\n# YouTube Video Explanation:\n\n# Coming Soon...\n\n <a href="https://youtu.be/Iq-IHUsIZNw">https://youtu.be/Iq-IHUsIZNw</a> \n\n\uD83D\uDD25 Please like, s
![Screenshot 2023-11-05 092159.png](https://assets.leetcode.com/users/images/a05970b6-3d25-449c-aeb6-b1e07e323802_1699156357.416551.png)\n\n# YouTube Video Explanation:\n\n# Coming Soon...\n\n<!-- [https://youtu.be/Iq-IHUsIZNw](https://youtu.be/Iq-IHUsIZNw) -->\n\n**\uD83D\uDD25 Please like, share, and subscribe to support our channel\'s mission of making complex concepts easy to understand.**\n\nSubscribe Link: https://www.youtube.com/@leetlogics/?sub_confirmation=1\n\n*Current Subscribers: 186*\n*Subscribe Goal: 200 Subscribers*\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem involves simulating a game between two integers in the array, where the larger integer wins and the smaller one moves to the end. We need to find the integer that wins k consecutive rounds.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize `winner` with the first element of the array and `wins` with 0 to keep track of consecutive wins.\n2. Iterate through the array from the second element (index 1).\n3. Compare the current `winner` with the current element in the array.\n4. If the `winner` is greater, increment `wins`, indicating a consecutive win.\n5. If the current element is greater, reset `wins` to 1, and update `winner` to the current element.\n6. Check if `wins` is equal to `k`. If so, break out of the loop since we\'ve found the winner.\n7. After the loop, return the `winner` as the result.\n\n# Complexity\n- Time Complexity: `O(n)` - We iterate through the array once, where n is the length of the array.\n- Space Complexity: `O(1)` - We use a constant amount of extra space to store variables, regardless of the input size.\n\nThe code efficiently finds the winner of the game and runs in linear time.\n\n# Code\n## Java\n```\nclass Solution {\n    public int getWinner(int[] arr, int k) {\n        int winner = arr[0];\n        int wins = 0;\n        \n        for (int i = 1; i < arr.length; i++) {\n            if (winner > arr[i])\n                wins++;\n            else {\n                wins = 1;\n                winner = arr[i];\n            }\n            \n            if (wins == k)\n                break;\n        }\n        \n        return winner;\n    }\n}\n```\n## C++\n```\nclass Solution {\npublic:\n    int getWinner(vector<int>& arr, int k) {\n        int winner = arr[0];\n        int wins = 0;\n        \n        for (int i = 1; i < arr.size(); i++) {\n            if (winner > arr[i])\n                wins++;\n            else {\n                wins = 1;\n                winner = arr[i];\n            }\n            \n            if (wins == k)\n                break;\n        }\n        \n        return winner;\n    }\n};\n```\n\n## Python\n```\nclass Solution(object):\n    def getWinner(self, arr, k):\n        winner = arr[0]\n        wins = 0\n        \n        for i in range(1, len(arr)):\n            if winner > arr[i]:\n                wins += 1\n            else:\n                wins = 1\n                winner = arr[i]\n            \n            if wins == k:\n                break\n        \n        return winner\n```\n\n## JavaScript\n```\nvar getWinner = function(arr, k) {\n    let winner = arr[0];\n    let wins = 0;\n    \n    for (let i = 1; i < arr.length; i++) {\n        if (winner > arr[i])\n            wins++;\n        else {\n            wins = 1;\n            winner = arr[i];\n        }\n        \n        if (wins == k)\n            break;\n    }\n    \n    return winner;\n};\n```\n---\n![upvote1.jpeg](https://assets.leetcode.com/users/images/7eb0e9d5-23fd-4f95-9384-f132c92031bc_1699156174.425512.jpeg)\n
['Array', 'Simulation', 'Python', 'C++', 'Java', 'JavaScript']