LeetCode Problem Workspace
Sum of Number and Its Reverse
Determine if a non-negative integer can be expressed as the sum of a number and its reverse using enumeration and math reasoning.
2
Topics
7
Code langs
3
Related
Practice Focus
Medium · Math plus Enumeration
Answer-first summary
Determine if a non-negative integer can be expressed as the sum of a number and its reverse using enumeration and math reasoning.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Math plus Enumeration
Start by iterating through all possible non-negative integers up to the target number. Reverse each candidate and check if its sum with the original equals the input number. Return true immediately on success, otherwise return false after exhausting all candidates, ensuring leading zeros in reversed numbers are handled correctly.
Problem Statement
Given a non-negative integer num, determine if it can be written as the sum of some non-negative integer x and its reverse. Return true if possible and false otherwise.
For example, for num = 443, check all integers from 0 up to 443 to see if x plus reverse(x) equals 443. Consider edge cases where reversing introduces leading zeros, such as 140 reversed being 041.
Examples
Example 1
Input: num = 443
Output: true
172 + 271 = 443 so we return true.
Example 2
Input: num = 63
Output: false
63 cannot be expressed as the sum of a non-negative integer and its reverse so we return false.
Example 3
Input: num = 181
Output: true
140 + 041 = 181 so we return true. Note that when a number is reversed, there may be leading zeros.
Constraints
- 0 <= num <= 105
Solution Approach
Brute-force Enumeration
Loop through every integer x from 0 to num. Reverse x using string conversion or arithmetic, then check if x plus reverse(x) equals num. This leverages the small constraint range efficiently.
Optimized Early Exit
As soon as a valid pair is found where x plus reverse(x) equals num, return true immediately to avoid unnecessary computation. This prevents iterating all the way to num in successful cases.
Handling Leading Zeros
When reversing numbers, handle leading zeros correctly, either by keeping them in string form or by ensuring integer conversion preserves the sum equivalence. This avoids false negatives due to ignored zero padding.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(num * k) where k is the number of digits in each candidate number, due to enumerating up to num and reversing each number. Space complexity is O(k) for temporary storage of the reversed number, either as string or digits array.
What Interviewers Usually Probe
- Check if candidate x plus its reverse equals num
- Expect early exit once solution is found
- Consider how leading zeros affect the reversed number sum
Common Pitfalls or Variants
Common pitfalls
- Ignoring leading zeros when reversing numbers
- Assuming only certain ranges of x need checking
- Not exiting early after finding a valid pair
Follow-up variants
- Find all x values that satisfy x plus reverse(x) equals num
- Restrict x to even or odd numbers and check sum
- Apply same logic to arrays where each element must satisfy the sum-with-reverse condition
FAQ
What is the key pattern in Sum of Number and Its Reverse?
The key pattern is using Math plus Enumeration to check all possible numbers and their reversed sums against the input.
How do leading zeros affect this problem?
Leading zeros appear when reversing numbers, and must be considered to ensure sums match correctly, especially for numbers like 140 reversed to 041.
What is the most efficient way to solve this problem?
Use brute-force enumeration with early exit once a valid x is found, avoiding unnecessary iterations up to num.
Can the solution handle the maximum constraint num = 105?
Yes, the small constraint allows checking all numbers up to 105 without performance issues.
Is there a way to find all numbers that satisfy the sum with reverse?
Yes, iterate through all candidates up to num and collect each x where x plus reverse(x) equals num, ensuring leading zeros are accounted for.
Solution
Solution 1: Brute Force Enumeration
Enumerate $k$ in the range $[0,.., num]$, and check whether $k + reverse(k)$ equals $num$.
class Solution:
def sumOfNumberAndReverse(self, num: int) -> bool:
return any(k + int(str(k)[::-1]) == num for k in range(num + 1))Continue Topic
math
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Math plus Enumeration
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Medium
Stay on this level to stabilize interview delivery.
arrow_forward