Understand Find the Punishment Number Problem

Find the Punishment Number

Description

Given a positive integer ( n ), return its punishment number.

The punishment number of ( n ) is the sum of squares of all numbers ( i ) (where ( 1in1 \leq i \leq n )) that satisfy:

  • ( i2i^2 ) can be split into contiguous substrings,
  • The sum of these substrings equals ( i ).

Example 1

Input:

n = 12

Output:

182

Explanation: Numbers satisfying the condition:

  • ( 12=11^2 = 1 ) ✅
  • (92=818+1=99^2 = 81 \rightarrow 8 + 1 = 9) ✅
  • ( 102=10010+0=1010^2 = 100 \rightarrow 10 + 0 = 10 ) ✅

Total punishment number: ( 1 + 81 + 100 = 182 )


Example 2

Input:

n = 25

Output:

182

Explanation: Numbers satisfying the condition:

  • (12=1 1^2 = 1 ) ✅
  • (92=818+1=99^2 = 81 \rightarrow 8 + 1 = 9) ✅
  • (102=10010+0=10 10^2 = 100 \rightarrow 10 + 0 = 10 ) ✅

Total punishment number: ( 1 + 81 + 100 = 182 )


Constraints

1n10001 \leq n \leq 1000

Category:
  • Graphs
Programming Language:
  • Java
Reference Link:

https://leetcode.com/problems/find-the-punishment-number-of-an-integer/description/

Java
Output:

Loading component...

Loading component...