Minimum number of elements that should be removed to make the array good | Function to return the minimum number of elements that must be removed to make the given array good ; Count frequency of each element ; For each element check if there exists another element that makes a valid pair ; If does not exist then increment answer ; Driver Code
def minimumRemoval ( n , a ) : NEW_LINE INDENT c = dict . fromkeys ( a , 0 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT c [ a [ i ] ] += 1 ; NEW_LINE DEDENT ans = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT ok = False ; NEW_LINE for j in range ( 31 ) : NEW_LINE INDENT x = ( 1 << j ) - a [ i ] ; NEW_LINE if ( x in c and ( c [ x ] > 1 or ( c [ x ] == 1 and x != a [ i ] ) ) ) : NEW_LINE INDENT ok = True ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( not ok ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 4 , 7 , 1 , 5 , 4 , 9 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( minimumRemoval ( n , a ) ) ; NEW_LINE DEDENT