✅☑[C++/Java/Python/JavaScript] || 2 Approaches || EXPLAINED🔥
PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n(Also explained in the code)\n\n#### Approach 1(With Queue)\n1. Initialization:\n\n    - The code initializ
# PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n\n#### ***Approach 1(With Queue)***\n1. **Initialization:**\n\n    - The code initializes a variable `maxElement` to store the maximum element in the `arr`. It starts with the first element of the array.\n    - A queue named `queue` is initialized to store the remaining elements in the array.\n1. **Processing the Array:**\n\n    - A `for` loop iterates through the elements of the array from the second element (index 1) onwards.\n    - In each iteration, the code checks if the current element is greater than the `maxElement`. If it is, the `maxElement` is updated.\n    - The current element (the winner of the previous comparison) is stored in the `curr` variable, and the current element is removed from the queue.\n1. **Comparing Elements:**\n\n    - The code enters a `while` loop that continues until the queue is not empty.\n    - In each iteration of the loop, the code:\n        - Retrieves the opponent\'s element from the front of the queue.\n        - Compares the `curr` element with the opponent\'s element to determine the winner.\n        - If `curr` is greater, it is pushed back to the end of the queue, and a "winstreak" counter is incremented.\n        - If the opponent\'s element is greater, it becomes the new `curr`, and the winstreak counter is reset to 1.\n1. **Winning Streak or Maximum Element:**\n\n    - After each comparison, the code checks if the winstreak has reached the desired value `k` or if `curr` has become the `maxElement`.\n    - If either condition is met, the function returns the current element `curr` as the winner of the game.\n1. **No Unique Winner:**\n\n    - If the loop completes without finding a winner (i.e., no winstreak of length `k` occurs), the function returns -1, indicating that there is no unique winner.\n\n\n# Complexity\n- *Time complexity:*\n   $$O(n)$$\n    \n\n- *Space complexity:*\n   $$O(n)$$\n    \n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int getWinner(vector<int>& arr, int k) {\n        int maxElement = arr[0];\n        queue<int> queue;\n        for (int i = 1; i < arr.size(); i++) {\n            maxElement = max(maxElement, arr[i]);\n            queue.push(arr[i]);\n        }\n        \n        int curr = arr[0];\n        int winstreak = 0;\n        \n        while (!queue.empty()) {\n            int opponent = queue.front();\n            queue.pop();\n            \n            if (curr > opponent) {\n                queue.push(opponent);\n                winstreak++;\n            } else {\n                queue.push(curr);\n                curr = opponent;\n                winstreak = 1;\n            }\n            \n            if (winstreak == k || curr == maxElement) {\n                return curr;\n            }\n        }\n        \n        return -1;\n    }\n};\n\n```\n\n```C []\n\n#include <stdlib.h>\n#include <stdio.h>\n#include <limits.h>\n\nint max(int a, int b) {\n    return (a > b) ? a : b;\n}\n\nint getWinner(int *arr, int arrSize, int k) {\n    int maxElement = arr[0];\n    int *queue = (int *)malloc((arrSize - 1) * sizeof(int));\n    int front = 0;\n    int rear = 0;\n\n    for (int i = 1; i < arrSize; i++) {\n        maxElement = max(maxElement, arr[i]);\n        queue[rear] = arr[i];\n        rear++;\n    }\n\n    int curr = arr[0];\n    int winstreak = 0;\n\n    while (front != rear) {\n        int opponent = queue[front];\n        front++;\n\n        if (curr > opponent) {\n            queue[rear] = opponent;\n            rear++;\n            winstreak++;\n        } else {\n            queue[rear] = curr;\n            rear++;\n            curr = opponent;\n            winstreak = 1;\n        }\n\n        if (winstreak == k || curr == maxElement) {\n            free(queue);\n            return curr;\n        }\n    }\n\n    free(queue);\n    return -1;\n}\n\nint main() {\n    int arr[] = {2, 1, 3, 5, 4, 6, 7};\n    int k = 2;\n    int result = getWinner(arr, 7, k);\n    printf("The winner is: %d\\n", result);\n\n    return 0;\n}\n\n\n\n```\n```Java []\nclass Solution {\n    public int getWinner(int[] arr, int k) {\n        int maxElement = arr[0];\n        Queue<Integer> queue = new LinkedList();\n        for (int i = 1; i < arr.length; i++) {\n            maxElement = Math.max(maxElement, arr[i]);\n            queue.offer(arr[i]);\n        }\n        \n        int curr = arr[0];\n        int winstreak = 0;\n        \n        while (!queue.isEmpty()) {\n            int opponent = queue.poll();\n            \n            if (curr > opponent) {\n                queue.offer(opponent);\n                winstreak++;\n            } else {\n                queue.offer(curr);\n                curr = opponent;\n                winstreak = 1;\n            }\n            \n            if (winstreak == k || curr == maxElement) {\n                return curr;\n            }\n        }\n        \n        return -1;\n    }\n}\n\n```\n\n```python3 []\nclass Solution:\n    def getWinner(self, arr: List[int], k: int) -> int:\n        max_element = max(arr)\n        queue = deque(arr[1:])\n        curr = arr[0]\n        winstreak = 0\n\n        while queue:\n            opponent = queue.popleft()\n            if curr > opponent:\n                queue.append(opponent)\n                winstreak += 1\n            else:\n                queue.append(curr)\n                curr = opponent\n                winstreak = 1\n            \n            if winstreak == k or curr == max_element:\n                return curr\n\n```\n\n```javascript []\nvar getWinner = function(arr, k) {\n    let maxElement = arr[0];\n    let queue = [];\n    \n    for (let i = 1; i < arr.length; i++) {\n        maxElement = Math.max(maxElement, arr[i]);\n        queue.push(arr[i]);\n    }\n    \n    let curr = arr[0];\n    let winstreak = 0;\n    \n    while (queue.length > 0) {\n        let opponent = queue.shift();\n        \n        if (curr > opponent) {\n            queue.push(opponent);\n            winstreak++;\n        } else {\n            queue.push(curr);\n            curr = opponent;\n            winstreak = 1;\n        }\n        \n        if (winstreak === k || curr === maxElement) {\n            return curr;\n        }\n    }\n    \n    return -1;\n};\n\n\n```\n\n---\n\n#### ***Approach 2(Without Queue)***\n1. The `getWinner` function takes two parameters: a vector `arr` of integers representing the players\' scores and an integer `k` representing the required win streak.\n\n1. It initializes `maxElement` with the first element of the array `arr`, which is used to keep track of the maximum score in the array.\n\n1. It iterates through the elements of the array to find the maximum score. The `maxElement` variable is updated to store the maximum score in the array.\n\n1. It initializes `curr` with the first element of the array, representing the current player\'s score.\n\n1. It also initializes a variable `winstreak` to 0, which will be used to count the consecutive wins of the current player.\n\n1. The function then iterates through the array from the second element (index 1) to the end.\n\n1. For each player (element) in the array, it compares the current player\'s score (`curr`) with the opponent\'s score. If the current player\'s score is greater than the opponent\'s, the `winstreak` is incremented, indicating a win. Otherwise, the `curr` variable is updated to the opponent\'s score, and the `winstreak` is reset to 1.\n\n1. After each comparison, the code checks if the `winstreak` is equal to `k` or if the current player\'s score is equal to the `maxElement`. If either condition is met, the function returns the current player\'s score, indicating that the player has won the required number of consecutive games or has achieved the maximum score.\n\n1. If the loop finishes without finding a winner, the function returns -1, indicating that there is no unique winner.\n\n\n# Complexity\n- *Time complexity:*\n   $$O(n)$$\n    \n\n- *Space complexity:*\n   $$O(1)$$\n    \n\n\n# Code\n```C++ []\n\nclass Solution {\npublic:\n    int getWinner(vector<int>& arr, int k) {\n        int maxElement = arr[0];\n        for (int i = 1; i < arr.size(); i++) {\n            maxElement = max(maxElement, arr[i]);\n        }\n        \n        int curr = arr[0];\n        int winstreak = 0;\n        \n        for (int i = 1; i < arr.size(); i++) {\n            int opponent = arr[i];\n            \n            if (curr > opponent) {\n                winstreak++;\n            } else {\n                curr = opponent;\n                winstreak = 1;\n            }\n            \n            if (winstreak == k || curr == maxElement) {\n                return curr;\n            }\n        }\n        \n        return -1;\n    }\n};\n```\n\n```C []\n\n#include <stdio.h>\n#include <limits.h>\n\nint max(int a, int b) {\n    return (a > b) ? a : b;\n}\n\nint getWinner(int arr[], int arrSize, int k) {\n    int maxElement = arr[0];\n    for (int i = 1; i < arrSize; i++) {\n        maxElement = max(maxElement, arr[i]);\n    }\n\n    int curr = arr[0];\n    int winstreak = 0;\n\n    for (int i = 1; i < arrSize; i++) {\n        int opponent = arr[i];\n\n        if (curr > opponent) {\n            winstreak++;\n        } else {\n            curr = opponent;\n            winstreak = 1;\n        }\n\n        if (winstreak == k || curr == maxElement) {\n            return curr;\n        }\n    }\n\n    return -1;\n}\n\nint main() {\n    int arr[] = {2, 1, 3, 5, 4, 6, 7};\n    int k = 2;\n    int result = getWinner(arr, 7, k);\n    printf("The winner is: %d\\n", result);\n\n    return 0;\n}\n\n\n\n```\n```Java []\nclass Solution {\n    public int getWinner(int[] arr, int k) {\n        int maxElement = arr[0];\n        for (int i = 1; i < arr.length; i++) {\n            maxElement = Math.max(maxElement, arr[i]);\n        }\n        \n        int curr = arr[0];\n        int winstreak = 0;\n        \n        for (int i = 1; i < arr.length; i++) {\n            int opponent = arr[i];\n            \n            if (curr > opponent) {\n                winstreak++;\n            } else {\n                curr = opponent;\n                winstreak = 1;\n            }\n            \n            if (winstreak == k || curr == maxElement) {\n                return curr;\n            }\n        }\n        \n        return -1;\n    }\n}\n\n```\n\n```python3 []\nclass Solution:\n    def getWinner(self, arr: List[int], k: int) -> int:\n        max_element = max(arr)\n        curr = arr[0]\n        winstreak = 0\n\n        for i in range(1, len(arr)):\n            opponent = arr[i]\n            if curr > opponent:\n                winstreak += 1\n            else:\n                curr = opponent\n                winstreak = 1\n            \n            if winstreak == k or curr == max_element:\n                return curr\n\n```\n\n```javascript []\nfunction getWinner(arr, k) {\n    let maxElement = arr[0];\n    for (let i = 1; i < arr.length; i++) {\n        maxElement = Math.max(maxElement, arr[i]);\n    }\n\n    let curr = arr[0];\n    let winstreak = 0;\n\n    for (let i = 1; i < arr.length; i++) {\n        let opponent = arr[i];\n\n        if (curr > opponent) {\n            winstreak++;\n        } else {\n            curr = opponent;\n            winstreak = 1;\n        }\n\n        if (winstreak === k || curr === maxElement) {\n            return curr;\n        }\n    }\n\n    return -1;\n}\n\nconst arr = [2, 1, 3, 5, 4, 6, 7];\nconst k = 2;\nconst result = getWinner(arr, k);\nconsole.log(`The winner is: ${result}`);\n\n\n```\n\n---\n\n\n\n# PLEASE UPVOTE IF IT HELPED\n\n---\n---\n\n\n---
['C', 'C++', 'Java', 'Python3', 'JavaScript']