Ctrl K

Pairs Balanced by Their Mirrors

Medium
The mirror of a non-negative integer is the number formed by its digits in reverse order, with any leading zeros dropped. For example, the mirror of 507 is 705 and the mirror of 340 is 43. You are given an array of non-negative integers values. A pair of indices (i, j) with i < j is balanced when values[i] plus the mirror of values[j] equals values[j] plus the mirror of values[i]. Return the number of balanced pairs. The count can be huge, so return it modulo 10⁹ + 7.
Example 1
values=[12, 23, 9, 34] → 3

The balanced pairs are (0, 1): 12 + 32 = 23 + 21 = 44, (0, 3): 12 + 43 = 34 + 21 = 55 and (1, 3): 23 + 43 = 34 + 32 = 66. The 9 pairs with nothing, so the answer is 3.

Example 2
values=[31, 20, 53, 2] → 3

Pair (0, 2): 31 + 35 = 66 and 53 + 13 = 66, so it balances. No other pair does, so the answer is 1.

Example 3
values=[5, 707, 0] → 3

All three numbers equal their own mirrors, so each of the three pairs balances.

Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 0 ≤ values[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPhoto Frames of the Same Shape MediumPairs Off the Pace MediumNumbers and Their Mirrors MediumPrimes Along Grid Rays MediumCorners Made of Ones Medium
loading Python…
Input
values=[12, 23, 9, 34]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 103 tests on the server, hidden ones included.