Check if it ’ s possible to completely fill every container with same ball | A boolean function that returns true if it 's possible to completely fill the container else return false ; Base Case ; Backtracking ; Function to check the conditions ; Storing frequencies ; Function Call for backtracking ; Driver Code ; Given Input ; Function Call
def getans ( i , v , q ) : NEW_LINE INDENT if ( i == len ( q ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT for j in range ( len ( v ) ) : NEW_LINE INDENT if ( v [ j ] >= q [ i ] ) : NEW_LINE INDENT v [ j ] -= q [ i ] NEW_LINE if ( getans ( i + 1 , v , q ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT v [ j ] += q [ i ] NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def Check ( c , b ) : NEW_LINE INDENT m = { } NEW_LINE for i in range ( len ( b ) ) : NEW_LINE INDENT if b [ i ] in m : NEW_LINE INDENT m [ b [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ b [ i ] ] = 1 NEW_LINE DEDENT DEDENT v = [ ] NEW_LINE for key , value in m . items ( ) : NEW_LINE INDENT v . append ( value ) NEW_LINE DEDENT check = getans ( 0 , v , c ) NEW_LINE if ( check ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT c = [ 1 , 3 , 3 ] NEW_LINE b = [ 2 , 2 , 2 , 2 , 4 , 4 , 4 ] NEW_LINE Check ( c , b ) NEW_LINE DEDENT