Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
Given a non-negative integer num, return true if num can be expressed as the sum of any non-negative integer and its reverse, or false otherwise.
Example 1:
Input: num = 443 Output: true Explanation: 172 + 271 = 443 so we return true.
Example 2:
Input: num = 63 Output: false Explanation: 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 Explanation: 140 + 041 = 181 so we return true. Note that when a number is reversed, there may be leading zeros.
Constraints:
0 <= num <= 105Problem summary: Given a non-negative integer num, return true if num can be expressed as the sum of any non-negative integer and its reverse, or false otherwise.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
443
63
181
sum-of-numbers-with-units-digit-k)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2443: Sum of Number and Its Reverse
class Solution {
public boolean sumOfNumberAndReverse(int num) {
for (int x = 0; x <= num; ++x) {
int k = x;
int y = 0;
while (k > 0) {
y = y * 10 + k % 10;
k /= 10;
}
if (x + y == num) {
return true;
}
}
return false;
}
}
// Accepted solution for LeetCode #2443: Sum of Number and Its Reverse
func sumOfNumberAndReverse(num int) bool {
for x := 0; x <= num; x++ {
k, y := x, 0
for k > 0 {
y = y*10 + k%10
k /= 10
}
if x+y == num {
return true
}
}
return false
}
# Accepted solution for LeetCode #2443: Sum of Number and Its Reverse
class Solution:
def sumOfNumberAndReverse(self, num: int) -> bool:
return any(k + int(str(k)[::-1]) == num for k in range(num + 1))
// Accepted solution for LeetCode #2443: Sum of Number and Its Reverse
impl Solution {
pub fn sum_of_number_and_reverse(num: i32) -> bool {
for i in 0..=num {
if i + ({
let mut t = i;
let mut j = 0;
while t > 0 {
j = j * 10 + (t % 10);
t /= 10;
}
j
}) == num
{
return true;
}
}
false
}
}
// Accepted solution for LeetCode #2443: Sum of Number and Its Reverse
function sumOfNumberAndReverse(num: number): boolean {
for (let i = 0; i <= num; i++) {
if (i + Number([...(i + '')].reverse().join('')) === num) {
return true;
}
}
return false;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.