Last Updated: 3 Jan, 2026

Digit Sum Pairs

Easy

Problem statement

You are given a non-empty array of unique, non-zero positive integers. Your task is to find the number of unique pairs of integers from this array where both numbers in the pair have the same sum of their digits.


For example, the number 23 has a digit sum of 2 + 3 = 5, and the number 50 also has a digit sum of 5 + 0 = 5. Therefore, (23, 50) would be a valid pair.


You need to calculate and print the total count of such unique pairs. If no such pairs can be formed, you should print -1.


Input Format:
The first line of input contains an integer N, the number of elements in the array.

The second line contains N space-separated integers, representing the elements of the array.


Output Format:
The output should be a single integer:
- The total number of unique pairs, if any exist.
- -1 if no valid pairs can be formed.


Note:
The input array contains unique positive integers.
A pair (a, b) is the same as (b, a). For example, if you count the pair (23, 50), you should not also count (50, 23).
The numbers within a pair must be different.