- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have an array A of positive integers, the elements are unique, now, two players P and Q are playing a game. At each move, any one player picks two numbers a and b from the array and if |a – b| is not in the array after that the player adds this number to the array. When a player cannot make the move loses the game. We have to find the winner of the game if player P always starts the game.

So, if the input is like A = [8,9,10], then the output will be P.

To solve this, we will follow these steps −

n := size of arr

g := arr[0], max_val := arr[0]

for i in range 1 to n, do

g := gcd(g, arr[i])

max_val := maximum of max_val, arr[i]

total :=(max_val / g) - n

if total is odd, then

return 'P'

return 'Q'

Let us see the following implementation to get better understanding −

from math import gcd def who_is_the_winner(arr) : n = len(arr) g = arr[0] max_val = arr[0] for i in range(1, n) : g = gcd(g, arr[i]) max_val = max(max_val, arr[i]) total = (max_val / g) - n if (total % 2 == 1) : return 'P' return 'Q' arr = [8,9,10] print(who_is_the_winner(arr))

[8,9,10]

P

- Related Questions & Answers
- Absolute Difference of all pairwise consecutive elements in an array (C++)?
- Program to find the winner of an array game using Python
- How to find the cross product of two vectors in R by adding the elements?
- Check if the elements of stack are pairwise sorted in Python
- Program to find winner of array removal game in Python
- Program to find minimum possible difference of indices of adjacent elements in Python
- Find the sum of maximum difference possible from all subset of a given array in Python
- Adding elements of an array until every element becomes greater than or equal to k in C++.
- Predict the winner of the game on the basis of the absolute difference of sum by selecting numbers in C++
- How to find the absolute pairwise difference among values of a vector in R?
- Maximize the median of the given array after adding K elements to the same array in C++
- Adding elements to the end of the ArrayList in C#
- C++ program to Adding elements of an array until every element becomes greater than or equal to k
- Program to find winner of stone game in Python
- Check if Queue Elements are pairwise consecutive in Python

Advertisements