LeetCode 2443: Sum of Number and Its Reverse Solution

Master LeetCode problem 2443 (Sum of Number and Its Reverse), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2443. Sum of Number and Its Reverse

Problem Explanation

Explanation:

To solve this problem, we need to iterate through all possible values up to half of the given number and check if the sum of the number and its reverse equals the given number. We can reverse a number by using modulo and division operations.

  1. Iterate from 0 to num/2.
  2. For each iteration, calculate the reverse of the current number.
  3. Check if the sum of the number and its reverse equals the given number.
  4. If a match is found, return true. Otherwise, return false at the end.

Time complexity: O(log(num)) - number of digits in num Space complexity: O(1) :

Solution Code

class Solution {
    public boolean isSumOfNumberAndReverse(int num) {
        for (int i = 0; i <= num / 2; i++) {
            int reverse = 0;
            int n = i;
            while (n > 0) {
                reverse = reverse * 10 + n % 10;
                n = n / 10;
            }
            if (i + reverse == num) {
                return true;
            }
        }
        return false;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2443 (Sum of Number and Its Reverse)?

This page provides optimized solutions for LeetCode problem 2443 (Sum of Number and Its Reverse) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

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

The time complexity for LeetCode 2443 (Sum of Number and Its Reverse) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2443 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2443 in Java, C++, or Python.

Back to LeetCode Solutions