Count pairs in an array having sum of elements with their respective sum of digits equal | Function to find the sum of digits of the number N ; Stores the sum of digits ; If the number N is greater than 0 ; Return the sum ; Function to find the count of pairs such that arr [ i ] + sumOfDigits ( arr [ i ] ) is equal to ( arr [ j ] + sumOfDigits ( arr [ j ] ) ; Stores the frequency of value of arr [ i ] + sumOfDigits ( arr [ i ] ) ; Traverse the given array ; Find the value ; Increment the frequency ; Stores the total count of pairs ; Traverse the map mp ; Update the count of pairs ; Return the total count of pairs ; Driver Code
def sumOfDigits ( N ) : NEW_LINE INDENT sum = 0 NEW_LINE while ( N ) : NEW_LINE INDENT sum += ( N % 10 ) NEW_LINE N = N // 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT def CountPair ( arr , n ) : NEW_LINE INDENT mp = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT val = arr [ i ] + sumOfDigits ( arr [ i ] ) NEW_LINE if val in mp : NEW_LINE INDENT mp [ val ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ val ] = 1 NEW_LINE DEDENT DEDENT count = 0 NEW_LINE for key , value in mp . items ( ) : NEW_LINE INDENT val = key NEW_LINE times = value NEW_LINE count += ( ( times * ( times - 1 ) ) // 2 ) NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 105 , 96 , 20 , 2 , 87 , 96 ] NEW_LINE N = len ( arr ) NEW_LINE print ( CountPair ( arr , N ) ) NEW_LINE DEDENT