MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / Solution

Class Solution

LeetCode_problems/Winner of an Array Game/solution.cpp:1–18  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class Solution {
2public:
3 int getWinner(vector<int>& arr, int k) {
4 int count = 0; //taking counter to check how many times a number has won
5 while(count!=k){
6 if(arr[0] > arr[1]){
7 arr.push_back(arr[1]); //used to insert the smaller number at the end
8 arr.erase(arr.begin()+1); //used to remove the smaller number from its orignal position
9 count++; //increase the number won counter as the same number would have won
10 } else {
11 arr.push_back(arr[0]); //used to insert the smaller number at the end
12 arr.erase(arr.begin()); //used to remove the smaller number from its orignal position
13 count = 1; //making the won counter to 1 as a new number would have won in this iteration
14 }
15 }
16 return arr[0]; //return the number at the first index, as it is the winner for 'k' times
17 }
18};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected