98.89% || O(n) || Clever Array Traversal
\n#include <vector>\n#include <algorithm>\n\nclass Solution {\npublic:\n    // Function to determine the winner of the game\n    int getWinner(vector<int>& arr,
```\n#include <vector>\n#include <algorithm>\n\nclass Solution {\npublic:\n    // Function to determine the winner of the game\n    int getWinner(vector<int>& arr, int k) {\n        // If k is greater than or equal to the number of elements in the array,\n        // the winner is the largest element.\n        if (k >= arr.size()) {\n            // Sort the array to find the largest element\n            sort(arr.begin(), arr.end());\n            // The largest element is at the end after sorting\n            return arr.back();\n        }\n\n        int win = 0; // To keep track of the number of consecutive wins\n        int winner = arr[0]; // Assume the first element is the winner initially\n        int i = 0; // Index to iterate over the array\n\n        // Continue looping until we reach the end of the array\n        while (i < arr.size()) {\n            // If the current winner beats the next element\n            if (winner > arr[i]) {\n                win++; // Increment the win count for the current winner\n                arr.push_back(arr[i]); // Move the loser to the end of the array\n            }\n            // If the next element beats the current winner\n            else if (winner < arr[i]) {\n                win = 1; // Reset win count for the new winner\n                arr.push_back(winner); // Move the old winner to the end of the array\n                winner = arr[i]; // Update the current winner\n            }\n            \n            // If the current winner has won k times in a row, return the winner\n            if (win == k)\n                return winner;\n            i++; // Move to the next element\n        }\n\n        // If no winner is found, return -1 (should not happen with proper input)\n        return -1;\n    }\n};\n```\n\nEver wondered how you could find a winner in a game where the rules seem to loop indefinitely? Here\'s a clever solution that does just that without needing extra space!\n\nIn this approach, we simulate the game by iterating over the array, keeping track of the current winner and their streak of wins. If the current winner can beat the next challenger, their win streak increases. However, if they lose, the challenger becomes the new winner, and the streak resets. This continues until either the win streak equals k or we\'ve looped through the array.\n\nThe real trick lies in managing the array dynamically. Losers are pushed to the end, which allows us to keep all our elements without using additional data structures. This ensures that the space complexity remains constant!\n\nIn the edge case where k is larger than the number of players, the problem simplifies to finding the largest element. The code handles this efficiently by sorting the array and returning the last element.\n\nThis solution is elegant and efficient, sidestepping the potential pitfall of infinite looping without bloating space complexity. Dive into the code, and you\'ll appreciate the beauty of its simplicity!
['Array', 'C']