MediumNo. 2443

Sum of Number and Its Reverse — LeetCode 2443 Python Solution

Topics
Math, Enumeration
Reading time
2 min

The problem

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

Input
num = 443
Output
true
Explanation
172 + 271 = 443 so we return true.

Python solution

PythonSolution
class Solution:
    def sumOfNumberAndReverse(self, num: int) -> bool:
        return any(k + int(str(k)[::-1]) == num for k in range(num + 1))

Complexity

Time
O(n × log n)where n is the size of num
Space · auxiliary
O(1)

Pattern: Math and Number Theory

Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 2443. Sum of Number and Its Reverse is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math.

The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.

Related problems

Related problems
No.ProblemDifficulty
0204Count PrimesMedium
0479Largest Palindrome ProductHard
0829Consecutive Numbers SumHard
0906Super PalindromesHard
1925Count Square Sum TriplesEasy
1952Three DivisorsEasy

Frequently asked questions

How hard is LeetCode 2443. Sum of Number and Its Reverse?

LeetCode 2443. Sum of Number and Its Reverse is rated Medium on LeetCode.

What is the time complexity of LeetCode 2443. Sum of Number and Its Reverse?

The Python solution on this page runs in O(n × log n), where n is the size of num.

What is the space complexity of LeetCode 2443. Sum of Number and Its Reverse?

The Python solution on this page uses O(1) auxiliary space.

What topics does LeetCode 2443. Sum of Number and Its Reverse cover?

LeetCode 2443. Sum of Number and Its Reverse is tagged Math and Enumeration on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.