Find the winner of a game of removing any number of stones from the least indexed non | Function to find the winner of game between A and B ; win = 1 means B is winner win = 0 means A is winner ; If size is even , winner is B ; If size is odd , winner is A ; Stone will be removed by B ; B will take n - 1 stones from current pile having n stones and force A to pick 1 stone ; Stone will be removed by A ; A will take n - 1 stones from current pile having n stones and force B to pick 1 stone ; Print the winner accordingly ; Driver Code ; Given piles of stone ; Function call
def findWinner ( a , n ) : NEW_LINE INDENT win = 0 NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT win = 1 NEW_LINE DEDENT else : NEW_LINE INDENT win = 0 NEW_LINE DEDENT for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( i % 2 == 1 ) : NEW_LINE INDENT if ( win == 0 and a [ i ] > 1 ) : NEW_LINE INDENT win = 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( win == 1 and a [ i ] > 1 ) : NEW_LINE INDENT win = 0 NEW_LINE DEDENT DEDENT DEDENT if ( win == 0 ) : NEW_LINE INDENT print ( " A " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " B " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE findWinner ( arr , N ) NEW_LINE DEDENT