Count valid pairs in the array satisfying given conditions | Function to return total valid pairs ; Initialize count of all the elements ; frequency count of all the elements ; Add total valid pairs ; Exclude pairs made with a single element i . e . ( x , x ) ; Driver Code ; Function call to print required answer
def ValidPairs ( arr ) : NEW_LINE INDENT count = [ 0 ] * 121 NEW_LINE for ele in arr : NEW_LINE INDENT count [ ele ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE for eleX , countX in enumerate ( count ) : NEW_LINE INDENT for eleY , countY in enumerate ( count ) : NEW_LINE INDENT if eleX < eleY : NEW_LINE INDENT continue NEW_LINE DEDENT if ( abs ( eleX - eleY ) % 2 == 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT ans += countX * countY NEW_LINE if eleX == eleY : NEW_LINE INDENT ans -= countX NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 16 , 17 , 18 ] NEW_LINE print ( ValidPairs ( arr ) ) NEW_LINE