Two player game in which a player can remove all occurrences of a number | Python3 implementation for two player game in which a player can remove all occurrences of a number ; Function that print whether player1 can wins or loses ; Storing the number of occurrence of elements in unordered map ; Variable to check if the occurrence of repeated elements is >= 4 and multiple of 2 or not ; Count elements which occur more than once ; Driver code
from collections import defaultdict NEW_LINE def game ( v , n ) : NEW_LINE INDENT m = defaultdict ( int ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( v [ i ] not in m ) : NEW_LINE INDENT m [ v [ i ] ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ v [ i ] ] += 1 NEW_LINE DEDENT DEDENT count = 0 NEW_LINE check = 0 NEW_LINE for i in m . values ( ) : NEW_LINE INDENT if ( i > 1 ) : NEW_LINE INDENT if ( i >= 4 and i % 2 == 0 ) : NEW_LINE INDENT check += 1 NEW_LINE DEDENT count += 1 NEW_LINE DEDENT DEDENT if ( check % 2 != 0 ) : NEW_LINE INDENT flag = False NEW_LINE DEDENT if ( check % 2 != 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT elif ( n % 2 == 0 and count % 2 == 0 ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 2 , 2 , 3 , 3 , 5 ] NEW_LINE size = len ( arr ) NEW_LINE game ( arr , size ) NEW_LINE DEDENT