- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the winner of the game where X picks 1, then Y picks 2, then X picks 3, and so on
There are two players, X and Y, who are playing a game. X will start the first and can pick 1 stone from the set of an unlimited number of stones after that Y will start and can pick the 2 stones, then X will pick 3, and so on the game will go alternatively until the sum of the total stones picked by X is less than or equal to the given number A or the sum of total stones picked by Y is less than or equal to another given number B. If the current sum of any player is going more that given maximum value for any player than he cannot pick the stones and lose the game.
Input
int a = 7 , b = 6;
Output
Y is the winner
Explanation: First X will remove the 1 stone, then Y will pick 2 stones, again X will pick 3 stones and Y will pick 4 stones.
Count of stones of X is 4 and if he will pick 5 more then it will exceed 7, so y is the winner.
Approach 1
In this approach, we will traverse over the while loop and will add the current number of stones to both x and y.
Example
#include <bits/stdc++.h> using namespace std; // function to find the answer int findWinner(int a, int b){ // creating variables to store the current number of stones for both the player's int sumX = 0, sumY = 0; int current = 1; // variable to store the current count of stones // traversing over the loop while (sumX <= a && sumY <= b){ if(current & 1){ // x will always get the odd number of stones sumX += current; } else{ sumY += current; // y will always get the even number of stones } current = current + 1; // increasing the current value } if(sumX > a){ return 2; // indicating 2 (Y) is winner } else{ return 1; // indicating 1 (X) is winner } } int main(){ int a = 7, b = 5; // given upper limits // calling the function if(findWinner(a,b) == 1){ cout<<"X is the winner of the game"<<endl; } else{ cout<<"Y is the winner of the game"<<endl; } return 0; }
Output
X is the winner of the game
Time and Space Complexity
The time complexity of the above code is O(sqrt(min(A, B))), where A and B are the given number of steps. Here, we increase the count at each step and the addition brings the factor of square root.
The space complexity of the above code is O(1), as we are not using any extra space.
Approach 2
In this approach, we will traverse over the for loop and will add the current number of stones to both x and y.
Example
#include <bits/stdc++.h> using namespace std; // function to find the answer int findWinner(int a, int b){ // creating variables to store the current number // of stones for both the players int sumX = 0, sumY = 0; // traversing over the loop for(int i=1; sumX <= a && sumY <=b; i++){ if(i & 1){ sumX += i; } else{ sumY += i; } } if(sumX > a){ return 2; // indicating 2 (Y) is winner } else{ return 1; // indicating 1 (X) is winner } } int main(){ int a = 7, b = 6; // given upper limits // calling the function if(findWinner(a,b) == 1){ cout<<"X is the winner of the game"<<endl; } else{ cout<<"Y is the winner of the game"<<endl; } return 0; }
Output
Y is the winner of the game
Time and Space Complexity
The time complexity of the above code is O(sqrt(min(A, B))), as we have just changed the looping method from previous code.
The space complexity of the above code is O(1), as we are not using any extra space.
Approach 3
In this approach, we will use the mathematical formula to get the maximum possible value of x and y and later compare them.
Example
#include <bits/stdc++.h> using namespace std; int findWinner(int a, int b){ // creating variables to store the current number of stones for both the players int sumX = 0, sumY = 0; int maxA = sqrt(a); int maxB = (sqrt(4 * b + 1) - 1)/2; if(maxA*maxA == a){ maxA++; } if((maxB * (maxB + 1)) == b){ maxB++; } if(maxA <= maxB){ return 2; // indicating 2 (Y) is winner } else{ return 1; // indicating 1 (X) is winner } } int main(){ int a = 7, b = 6; // given upper limits if(findWinner(a,b) == 1){ cout<<"X is the winner of the game"<<endl; } else{ cout<<"Y is the winner of the game"<<endl; } return 0; }
Output
Y is the winner of the game
Time and Space Complexity
The time complexity of the above code is O(log(A + B)), because we are using the sqrt method which works in logarithmic time.
The space complexity of the above code is O(1), as we are not using any extra space.
Conclusion
In this tutorial, we have implemented three different approaches to find the winner among two players who are playing a game by first person picking odd number of stones and another even number of consecutive stones up to the maximum sum of given numbers A and B. Which player is first unable to pick the stones will lose.