A billing system stores n non-negative invoice amounts. Two invoices belong to the same "digit-sum bracket" if the sum of the decimal digits of their amounts is identical (for example, 12 and 111 both have digit sum 3, since 1+2=3 and 1+1+1=3).
Count the number of unordered pairs of invoices (i, j) with i < j whose digit sums are equal.
Line 1: an integer n.
Line 2: n space-separated non-negative integers (the invoice amounts).
A single integer: the number of pairs (i, j), i < j, whose digit sums match.
Example 1
Input
4 12 23 5 41
Expected
3
Explanation
Digit sums are 12→3, 23→5, 5→5, 41→5. The values 23, 5, and 41 all share digit sum 5, giving C(3,2)=3 matching pairs; 12 matches nothing. Total: 3.
Example 2
Input
3 1 2 3
Expected
0
Explanation
Digit sums are 1, 2, 3 — all different, so there are 0 matching pairs.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →