Compute the punishment number of an integer by checking which squares can be partitioned into decimal chunks that sum back to the original number.
Punishment Number
gfgGiven a positive integer , consider every integer from $1n$.
For each , square it to get . If the decimal representation of can be split into one or more contiguous substrings such that the integer values of those substrings add up exactly to , then contributes to the answer.
Return the sum of the squares of all such valid integers in the range .
The task is to determine this total punishment number.
Input Format
- A single integer .
Output Format
- Return the punishment number of , defined as the sum of for all whose square can be partitioned into decimal substrings summing to .
Constraints
- The exact upper bound is not provided in the source context; solutions should work efficiently for the typical LeetCode range.
- Only decimal substring partitions are allowed; substrings must be contiguous and use the digits of in order.
Example 1
Input
n = 10
Output
182
Explanation
Valid values are 1, 9, and 10.
- = 1 -> [1] sums to 1
- = 81 -> [8, 1] sums to 9
- = 100 -> [10, 0] sums to 10
So the answer is 1 + 81 + 100 = 182.
Example 2
Input
n = 1
Output
1
Explanation
Only 1 is checked, and = 1 can be split as [1], which sums to 1.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.