Simple O(n) solution with explanation
Instead of shifting the elements, treat the array as circular array and simulate the process, keeping the count of the win times of a particular number. When an
Instead of shifting the elements, treat the array as circular array and simulate the process, keeping the count of the win times of a particular number. When any particular element wins **k** times return the number.\n\n**Note:** If k>= n (length of the array) return the max element, since no other element can win n times. As after at most n iterations the max element will be at front and then it is only going to win consecutively. Any element other than the max element can win at most (n-1) times and hence can never be a answer.\n\n**Time Complexity:** O(n)\n**Space Complexity:** O(n), for keeping the win count of an element\n\nDo a try run on few test cases with k >= n to have a better understanding.\n\n```\nclass Solution {\npublic:\n    int getWinner(vector<int>& arr, int k) {\n        int n = arr.size();\n        if(k >= n) return *max_element(arr.begin(), arr.end());\n        \n        unordered_map<int, int> mp;\n        \n//         input array makes sure that array contains at least 2 elements\n        for(int i = 0; ; i++){\n            int winner = max(arr[i%n], arr[(i+1)%n]);\n            mp[winner]++;\n            if(mp[winner] == k)\n                return winner;\n            \n            if(arr[i%n] > arr[(i+1) % n]){\n                swap(arr[i%n], arr[(i+1)% n]);\n            }\n        }\n        return 0;\n    }\n};\n```
['C', 'Simulation']