2443. Sum of Number and Its Reverse LeetCode Solution

In this guide, you will get 2443. Sum of Number and Its Reverse LeetCode Solution with the best time and space complexity. The solution to Sum of Number and Its Reverse problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Sum of Number and Its Reverse solution in C++
  4. Sum of Number and Its Reverse solution in Java
  5. Sum of Number and Its Reverse solution in Python
  6. Additional Resources
2443. Sum of Number and Its Reverse LeetCode Solution image

Problem Statement of Sum of Number and Its Reverse

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

Complexity Analysis

  • Time Complexity: O(\texttt{num})
  • Space Complexity: O(1)

2443. Sum of Number and Its Reverse LeetCode Solution in C++

class Solution {
 public:
  bool sumOfNumberAndReverse(int num) {
    for (int i = num / 2; i <= num; ++i)
      if (num == i + reversed(i))
        return true;
    return false;
  }

 private:
  int reversed(int num) {
    int ans = 0;
    while (num > 0) {
      ans = ans * 10 + num % 10;
      num /= 10;
    }
    return ans;
  }
};
/* code provided by PROGIEZ */

2443. Sum of Number and Its Reverse LeetCode Solution in Java

N/A
// code provided by PROGIEZ

2443. Sum of Number and Its Reverse LeetCode Solution in Python

N/A
# code by PROGIEZ

Additional Resources

See also  3425. Longest Special Path LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.