2698. Find the Punishment Number of an Integer LeetCode Solution
In this guide, you will get 2698. Find the Punishment Number of an Integer LeetCode Solution with the best time and space complexity. The solution to Find the Punishment Number of an Integer problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find the Punishment Number of an Integer solution in C++
- Find the Punishment Number of an Integer solution in Java
- Find the Punishment Number of an Integer solution in Python
- Additional Resources
Problem Statement of Find the Punishment Number of an Integer
Given a positive integer n, return the punishment number of n.
The punishment number of n is defined as the sum of the squares of all integers i such that:
1 <= i <= n
The decimal representation of i * i can be partitioned into contiguous substrings such that the sum of the integer values of these substrings equals i.
Example 1:
Input: n = 10
Output: 182
Explanation: There are exactly 3 integers i in the range [1, 10] that satisfy the conditions in the statement:
– 1 since 1 * 1 = 1
– 9 since 9 * 9 = 81 and 81 can be partitioned into 8 and 1 with a sum equal to 8 + 1 == 9.
– 10 since 10 * 10 = 100 and 100 can be partitioned into 10 and 0 with a sum equal to 10 + 0 == 10.
Hence, the punishment number of 10 is 1 + 81 + 100 = 182
Example 2:
Input: n = 37
Output: 1478
Explanation: There are exactly 4 integers i in the range [1, 37] that satisfy the conditions in the statement:
– 1 since 1 * 1 = 1.
– 9 since 9 * 9 = 81 and 81 can be partitioned into 8 + 1.
– 10 since 10 * 10 = 100 and 100 can be partitioned into 10 + 0.
– 36 since 36 * 36 = 1296 and 1296 can be partitioned into 1 + 29 + 6.
Hence, the punishment number of 37 is 1 + 81 + 100 + 1296 = 1478
Constraints:
1 <= n <= 1000
Complexity Analysis
- Time Complexity: O(n^2)
- Space Complexity: O(n)
2698. Find the Punishment Number of an Integer LeetCode Solution in C++
class Solution {
public:
int punishmentNumber(int n) {
int ans = 0;
for (int i = 1; i <= n; ++i)
if (isPossible(0, 0, to_string(i * i), 0, i))
ans += i * i;
return ans;
}
private:
// Returns true if the sum of any split of `numChars` equals to the target.
bool isPossible(int accumulate, int running, const string& numChars, int s,
int target) {
if (s == numChars.length())
return target == accumulate + running;
const int d = numChars[s] - '0';
return isPossible(accumulate, running * 10 + d, numChars, s + 1, target) ||
isPossible(accumulate + running, d, numChars, s + 1, target);
}
};
/* code provided by PROGIEZ */
2698. Find the Punishment Number of an Integer LeetCode Solution in Java
class Solution {
public int punishmentNumber(int n) {
int ans = 0;
for (int i = 1; i <= n; ++i)
if (isPossible(0, 0, String.valueOf(i * i), 0, i))
ans += i * i;
return ans;
}
// Returns true if the sum of any split of `numChars` equals to the target.
private boolean isPossible(int accumulate, int running, String numChars, int s, int target) {
if (s == numChars.length())
return target == accumulate + running;
final int d = numChars.charAt(s) - '0';
return isPossible(accumulate, running * 10 + d, numChars, s + 1, target) ||
isPossible(accumulate + running, d, numChars, s + 1, target);
}
}
// code provided by PROGIEZ
2698. Find the Punishment Number of an Integer LeetCode Solution in Python
class Solution:
def punishmentNumber(self, n: int) -> int:
def isPossible(
accumulate: int, running: int, numChars: list[str],
s: int, target: int) -> bool:
"""
Returns True if the sum of any split of `numChars` equals to the target.
"""
if s == len(numChars):
return target == accumulate + running
d = int(numChars[s])
return (
# Keep growing `running`.
isPossible(accumulate, running * 10 + d, numChars, s + 1, target) or
# Start a new `running`.
isPossible(accumulate + running, d, numChars, s + 1, target)
)
return sum(i * i
for i in range(1, n + 1)
if isPossible(0, 0, str(i * i), 0, i))
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.