Skip to main content
Back to problems
Leetcode
Medium
Math
Backtracking
Recursion
Find The Punishment Number Of An Integer

Compute the punishment number of an integer by checking which squares can be partitioned into decimal chunks that sum back to the original number.

Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.

Punishment Number

gfg
Problem Statement

Given a positive integer nn, consider every integer ii from $1toton$.

For each ii, square it to get i2i^2. If the decimal representation of i2i^2 can be split into one or more contiguous substrings such that the integer values of those substrings add up exactly to ii, then ii contributes i2i^2 to the answer.

Return the sum of the squares of all such valid integers ii in the range [1,n][1, n].

The task is to determine this total punishment number.

Input Format

  • A single integer nn.

Output Format

  • Return the punishment number of nn, defined as the sum of i2i^2 for all 1in1 \le i \le n whose square can be partitioned into decimal substrings summing to ii.

Constraints

  • 1n1 \le n
  • 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 i2i^2 in order.
Examples
Sample cases returned by the problem API.

Example 1

Input

n = 10

Output

182

Explanation

Valid values are 1, 9, and 10.

  • 121^{2} = 1 -> [1] sums to 1
  • 929^{2} = 81 -> [8, 1] sums to 9
  • 10210^{2} = 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 121^{2} = 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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.