Minimum number of coins needed to remove all the elements of the array based on given rules | Function to calculate minimum number of coins needed ; Consider the first element separately , add 1 to the total if it 's of type 1 ; Iterate from the second element ; If the current element is of type 2 then any Player can remove the element ; Second pointer to reach end of type 1 elements ; Increment j until arr [ j ] is equal to 1 and j is not out of bounds ; Number of type 1 elements in a continious chunk ; From next iteration i pointer will start from index of j ; Return the minimum count of coins ; Driver Code
def minimumcoins ( arr , N ) : NEW_LINE INDENT coins = 0 NEW_LINE j = 0 NEW_LINE if ( arr [ 0 ] == 1 ) : NEW_LINE INDENT coins += 1 NEW_LINE DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT if ( arr [ i ] == 2 ) : NEW_LINE INDENT continue NEW_LINE DEDENT j = i NEW_LINE while ( j < N and arr [ j ] == 1 ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT x = ( j - i ) NEW_LINE coins += x // 3 NEW_LINE i = j - 1 NEW_LINE DEDENT return coins NEW_LINE DEDENT N = 8 NEW_LINE arr = [ 1 , 2 , 1 , 1 , 2 , 1 , 1 , 1 ] NEW_LINE print ( minimumcoins ( arr , N ) ) NEW_LINE