There are N coins and K people. Each of them can propose a method of distribution of the coins amongst themselves when their chance comes and a vote will occur in favour or against his distribution method between all those members. The person proposing that method of distribution wins the vote if he gets equal or more votes in favour than in against his proposal else he loses. Loosing he would be eliminated and then the next member will now propose his method of distribution amongst the remaining members.
Each person while proposing his method of distribution wants to get the maximum number of coins as well as win the vote.
Each person is smart and knows all the possibilities that may occur from their vote and will cast their vote accordingly.
The first proposal will always be given by 1 if he loses will be followed by 2 and so on (till the Kth person).
In the distribution of the 1^{st} person print the amount of coins each of K people is proposed to get so that he wins the vote.
Example 1:
Input:
N = 100 and K = 2
Output: 100 0
Explanation:
To get the maximum coins the 1^{st} person will propose the 
distribution 100,0 when the vote occurs he will obviously
vote for himself and the second person against him. 
The result of the vote will be 1-1 which means he will 
survive (tie here means victory) the vote.
 
Example 2:
Input:
N = 100 and K = 1
Output: 100
Your Task:  
You don't need to read input or print anything. Your task is to complete the function coinsGame() which takes the integer N and an integer K as input parameters and returns the K space-separated Integers denoting the distribution proposed by the 1^{st} person.
Expected Time Complexity: O(K)
Expected Auxiliary Space: O(1)
 
Constraints:
1<=N<=10^{9}
1<=K<=10^{4}
N>=K

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Game theory

Complete search

Starter code: #User function Template for python3





class Solution:

    def coinsGame (self, N, K):

        # code here

        





Solutions:
class Solution:

	def coinsGame(self, N, K):
		(ans, temp) = ([], [])
		result = [0] * K
		for i in range(2, K, 2):
			result[i] = 1
		result[0] = N - sum(result)
		ans.append(result)
		for i in ans:
			for j in i:
				temp.append(int(j))
		temp.append('')
		return temp


class Solution:

	def coinsGame(self, n, k):
		ans = [0 for _ in range(k)]
		p = (k - 1) // 2
		n -= p
		ans[0] = n
		for i in range(2, k, 2):
			ans[i] = 1
		return ans


class Solution:

	def coinsGame(self, N, k):
		if k == 1:
			return [N]
		if k == 2:
			return [N, 0]
		if k > 2:
			ans = [0 for i in range(k)]
			for i in range(1, k):
				if i % 2 == 0:
					ans[i] = 1
			leftover = N - sum(ans)
			ans[0] = leftover
		return ans


class Solution:

	def coinsGame(self, N, K):
		if K == 1:
			return [N]
		res = [N - (K - 1) // 2] + [0, 1] * ((K - 1) // 2)
		if K % 2 == 0:
			res += [0]
		res = [str(item) for item in res]
		return res


class Solution:

	def coinsGame(self, N, K):
		numberOfVotes = K // 2 + 1
		if K % 2 == 0:
			numberOfVotes = K // 2
		res = []
		res.append(N - numberOfVotes + 1)
		for i in range(1, K):
			if i % 2 == 1:
				res.append(0)
			else:
				res.append(1)
		return res


class Solution:

	def coinsGame(self, N, K):
		arr = [0] * K
		x = N - (K - 1) // 2
		arr[0] = x
		for i in range(2, K, 2):
			arr[i] = 1
		return arr


class Solution:

	def coinsGame(self, N, K):
		ans = [0] * K
		for i in range(K):
			if i % 2 == 0:
				ans[i] = 1
		s = sum(ans)
		c = N - s
		ans[0] += c
		return ans


class Solution:

	def coinsGame(self, N, K):
		L = []
		if K % 2 == 0:
			count = N - (K - 2) // 2
			L.append(count)
			for i in range(K - 1):
				if i % 2 == 0:
					L.append(0)
				else:
					L.append(1)
		else:
			count = N - (K - 1) // 2
			L.append(count)
			for i in range(K - 1):
				if i % 2 == 0:
					L.append(0)
				else:
					L.append(1)
		return L