Given an array A of N integers. Two players play a game with the array in turns. The game aims to make the array equal. Players add 1 to any number of the array to increase it by 1(except the maximum number). The player who adds last i.e. the player after whose turn the array becomes equal is declared the winner. If no moves can be made, the game is a draw.
Example 1:
Input:
N=2
A[]={1,2}
Output:
First
Explanation:
The first player adds 1 to the first 
number making it 2. The array 
becomes equal, so he is the winner.
Example 2:
Input:
N=3
A[]={2,2,2}
Output:
Draw
Explanation:
No moves can be made as all the elements are
already maximum and numbers cannot be
added to the maximum numbers.
Your Task:
You don't need to read input or print anything. Your task is to complete the function arrayGame() which takes an integer N and an integer array A as input parameters and returns a string denoting the result of the game. ("Draw" if the game is a draw, "First" if the first player and "Second" otherwise).
Expected Time Complexity: O(N)
Expected Auxillary Space: O(1)
Constraints:
1<=N<=10^{6}
1<=A[i]<=10^{9}

Source: geeksforgeeks | Difficulty: EASY
Tags: Game theory

Mathematics

Starter code: #User function Template for python3

class Solution:
    def arrayGame(self,N,A):
        #code here

Solutions:
class Solution:

	def arrayGame(self, N, A):
		x = max(A)
		a = N * x
		c = sum(A) - a
		if a == sum(A):
			return 'Draw'
		if c % 2 == 0:
			return 'Second'
		else:
			return 'First'


class Solution:

	def arrayGame(self, N, A):
		x = max(A)
		for i in range(N):
			if A[i] <= x:
				A[i] = x - A[i]
		s = sum(A)
		if s == 0:
			return 'Draw'
		elif s & 1 == 0:
			return 'Second'
		elif s & 1 != 0:
			return 'First'


class Solution:

	def arrayGame(self, N, A):
		A.sort()
		index = 0
		for i in range(N):
			if A[i] == A[N - 1]:
				index = i - 1
				break
		if index == -1:
			return 'Draw'
		turns = 0
		for i in range(0, index + 1):
			turns += A[N - 1] - A[i]
		if turns % 2 == 0:
			return 'Second'
		else:
			return 'First'


class Solution:

	def arrayGame(self, N, A):
		if len(set(A)) == 1:
			return 'Draw'
		max1 = max(A)
		count = 0
		for i in A:
			if i == max1:
				continue
			count += i - max1
		if count % 2 == 0:
			return 'Second'
		else:
			return 'First'


class Solution:

	def arrayGame(self, N, A):
		if sum(A) == len(A) * A[0]:
			return 'Draw'
		else:
			c = 0
			val = max(A)
			for k in range(N):
				c += val - A[k]
			if c % 2 == 1:
				return 'First'
			else:
				return 'Second'


class Solution:

	def arrayGame(self, N, A):
		totalSum = A[0]
		maxElement = A[0]
		for i in range(1, len(A)):
			maxElement = max(maxElement, A[i])
			totalSum += A[i]
		totalSum = maxElement * len(A) - totalSum
		if not totalSum:
			return 'Draw'
		if totalSum & 1:
			return 'First'
		return 'Second'