3Blue1Brown-styled visual simulation/dry-run of the problem using Deque
This problem can also be solved by actually simulating the condition mentioned in the problem using a deque. If you just want to see the visualizations for cert
This problem can also be solved by actually simulating the condition mentioned in the problem using a `deque`. If you just want to see the visualizations for certain test cases, they are towards the end. Basically, pop the first 2 elements, compare them, push the greater at front, lesser at back. As soon as the win_count is equal to k (or the maximum element is at the front), break. Here\'s the code:\n```\nclass Solution {\npublic:\n    int getWinner(vector<int>& arr, int k) {\n        const int n = arr.size();\n        deque<int> dq;\n        for(int i : arr) dq.push_back(i);\n        int last = 2e9;\n        int lc = 0;\n        int maxi = *max_element(arr.begin(), arr.end());\n        while(true){\n            int w;\n\t\t\t// pop the first 2 elements from the deque and compare them\n            int fst = dq.front(); dq.pop_front();\n            int sec = dq.front(); dq.pop_front();\n            if(fst > sec){\n                w = fst;\n            }\n            else {\n                w = sec;\n                \n            }\n            dq.push_front(max(fst, sec)); // push larger at front\n            dq.push_back(min(fst, sec)); // and smaller at back\n            if(w == last){                        // current winner is same as previous\n                lc++;\n                if(lc == k or w == maxi) return w; // value of k is reached or maximum element at front\n            }\n            else{                                     // current winner different from previous\n                last = w;\n                lc = 1;\n                if(lc == k or w == maxi) return w;\n            }\n            \n        }\n    }\n};\n```\nUnfortunately, LC doesn\'t seem to support GIFs, so sharing link of the visualizations here. Just so you know what the visualizations look like, sharing a static image here\nStatic image: ![image](https://assets.leetcode.com/users/images/041e3569-5811-4ae7-a8da-8da27c554cf4_1596623277.733051.png)\n\nTest case 1: https://gifyu.com/image/WyCH\nTest case 3: https://gifyu.com/image/WyUn\nTest case 4: https://gifyu.com/image/WyVn\n\nPS. If you like these kinds of simulations, an upvote would be helpful.\n
[]