Find the Punishment Number of an Integer

Medium Topics Company Tags

For each integer i from 1 through n, consider the decimal digits of i * i. You may split all of those digits into one or more nonempty consecutive pieces without changing their order. Interpret each piece as an integer; leading zeros are allowed.

If some split has pieces whose values sum to i, include i * i in the result. Return the sum of the included squares. Each qualifying square is counted once, even if several splits work.

Example 1:

Input: n = 9

Output: 82

Explanation: Only 1 and 9 qualify. Their squares are 1 and 81, and the splits 1 and 8 + 1 sum to 1 and 9 respectively. The result is 1 + 81 = 82.

Example 2:

Input: n = 8

Output: 1

Explanation: Only 1 qualifies among the integers from 1 through 8, so the result is its square, 1.

Example 3:

Input: n = 36

Output: 1478

Explanation: The qualifying integers are 1, 9, 10, and 36. For 36, split 1296 into 1 + 29 + 6 = 36. Their squares sum to 1 + 81 + 100 + 1296 = 1478.

Constraints:

  • 1 <= n <= 1000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.