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
function sumOfDigits ( N ) { let sum = 0 ; while ( N != 0 ) { sum = sum + ( N % 10 ) ; N = Math . floor ( N / 10 ) ; } return sum ; } function CountPair ( arr , n ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { let val = arr [ i ] + sumOfDigits ( arr [ i ] ) ; if ( mp . has ( val ) ) { mp . set ( val , mp . get ( val ) + 1 ) ; } else { mp . set ( val , 1 ) ; } } let count = 0 ; for ( let [ key , value ] of mp ) { count = count + ( value * ( value - 1 ) ) / 2 ; } return count ; } let arr = [ 105 , 96 , 20 , 2 , 87 , 96 ] ; let N = arr . length ; document . write ( CountPair ( arr , N ) )