Directi Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Phone Interview
I don't think " The person who goes first will always win" is true for the case below:
who will win if N=5 , A[0]=5 , A[1]=4 , A[2]=3 , A[3]=1, A[4]=2 ;
Good point. It seems we can fix that by saying 'take K-1 coins from the current pile if the next pile has 2 or more coins on it, otherwise take all K' - therefore we are still in the situation where the person who goes first wins.
If the first pile has 1 coin, however, I think the person who goes first will lose.
The person who goes first will always win except for a case when first pile contain only 1 coin.
In that case first one of both who reach to a pile with coins>1 will win.
For Ex. 1 1 1 2 3 1 1 3
in this case if alice plays first then bob will reach to pile[3] first which is >1,hence bob will win.
take N stacks, each having having equal no of elements. then take two arrays alice[ ] and bob[ ]. pop elements from the stack and insert them into alice[ ] and bob [] alternatively. when only one element in the last stack is remaining, check to see which array (alice [ ] or bob[ ]) has more no of elements. the array with the more no of elements wins !!
i think if we assume that no pile has 1coin then first player will always in.
as explained above the first player will always pick k-1 coing from ith pile if it has k coins.
> presence of 1 coin in a pile makes it interesting such that winning probability is inverted. i.e. if there is 1 pile having 1 coin then , the player playing second will win.
> so every pile having single coin initially inverts the winning possibility.
the solution should be: count the number of piles with 1 coin and and if they are odd second player wins and if they are even first player wins.
Alice can decide if she wants to play first or second ,which way she can win. If she wants to go first in next pile,then in current pile it should go first if no. of coins in current pile is more than 1 picking all except 1 so that Bob picks the left one and she starts first in next pile. If she wants to go second in next pile,which means Bob should go first in next pile so it must go first in this pile and pick all coins so Bob starts in first place in next pile. By doing so upto first pile if going first is what Alice wishes ,then she wins as she starts the game else Bob wins. so it can be solved using Dynamic Programming.See the code:
#define F 1 //first
#define S 2 //second
bool winner(int arr[],int n)
{
//arr[i] is no. of coins in ith pile
//n is the no. of piles
int Alice[n],state,i; //doing for alice
Alice[n-1] = ((arr[n-1] > 1)?F:S);
for(i = n-2; i >= 0; i--)
{
if(Alice[i+1] == F)
state = ((arr[i] > 1)?F:S);
else //Alice going second in (i+1)th pile
state = F; //pick all here
Alice[i] = state;
}
//in game Alice is starting first
if(Alice[0] == F)
return true;
else
return false; //bob wins
}
Alice wins iff the number of piles with 1 coin is odd. Bob otherwise.
Alice always take A[i]-1 (if A[i] > 1) coins from every pile. If pile i has only one coin then she has to take at least one coin per pile and then Bob will lead the game (he is capable now to take A[j] -1 coin first and leave Alice with just one coin to be picked up. Same Reasoning holds now for Bob.
Let's work backwards. In order to ensure there's precisely one coin left, we have to be the one to start the pile. That is, if a pile has K coins, and one person picks up K-1 of them, they will win since there is only 1 coin left.
- jay March 23, 2013So, we know that the winner of the game is the one who starts the last pile, therefore optimal plays says they will both want to start a pile.
How do you start a pile? Ensure there is just one coin left on the previous pile.
Seems to me, like we have an answer. The person who goes first will always win. They simply have to remove all but 1 coin on the first pile. Their opponent can only remove the final coin because that pile is the leftmost non-empty.
The person who goes first can now repeat this process. Eventually, their opponent will have to pick up the last coin, on the last pile.