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 <= 105
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach means we will check every number smaller than the input number. For each of those smaller numbers, we will calculate its reverse and then add it to the original number to see if it equals our input number.
Here's how the algorithm would work step-by-step:
def sum_of_number_and_its_reverse_brute_force(input_number):
current_number = 1
while current_number < input_number:
# Convert the current number to a string so it can be reversed.
string_representation = str(current_number)
reversed_string = string_representation[::-1]
# Convert the reversed string back to an integer for addition.
reversed_number = int(reversed_string)
sum_of_numbers = current_number + reversed_number
# Check if the sum is equal to the input number
if sum_of_numbers == input_number:
return True
current_number += 1
# No solution was found.
return False
The goal is to find how many numbers can be written as the sum of themselves and their reverse, excluding numbers that end in zero. We'll focus on cleverly constructing possible sums and verifying if they can be achieved, instead of checking every single number below the given limit.
Here's how the algorithm would work step-by-step:
def sum_of_number_and_reverse(input_number):
count = 0
for i in range(1, input_number + 1):
# Exclude numbers ending in zero as per the problem description.
if i % 10 == 0:
continue
reversed_i = int(str(i)[::-1])
sum_of_i_and_reversed_i = i + reversed_i
# Only count the numbers within the input_number limit
if sum_of_i_and_reversed_i <= input_number:
count += 1
return count
Case | How to Handle |
---|---|
Input number is 0 | Return true immediately as 0 + reverse(0) = 0 |
Input number is negative | Return false, as reversing a negative number and adding it will never equal a positive number. |
Input number consists of all 0s (e.g., 000) | Treat as 0 after removing leading zeros during the reverse operation |
Input number is a palindrome | Check if num+num equals original number to return true, if not then proceed with normal reverse process |
Input number results in integer overflow upon reversal | Catch the overflow exception or check if reversed number is too large, returning false in these cases. |
Input is a very large number close to integer limits | Be mindful of potential integer overflow during the addition of the number and its reverse. |
The reverse operation results in a number with leading zeros | Ensure the leading zeros are trimmed from reversed number before addition to avoid incorrect result |
No solution exists (e.g., very large number) | Return false when iterating through all possible numbers up to the input number produces no solution. |