Talentd
FleetCode
Make and follow sheets.Sign in

Sum of Number and Its Reverse solution and explanation

MediumEnumerationMath5 min read50.0% acceptanceUpdated 9 Apr 2026
2443. Sum of Number and Its Reverse | Leetcode Weekly 315 | LeetCode 2443Bro Coders · 3:42 · 1K viewsWatch on YouTube
9 more videos on this problem
  1. 2443. Sum of Number and Its Reverse | Leetcode Weekly 315 | LeetCode 2443Bro Coders · 3:42 · 1K views
  2. 2443. Sum of Number and Its Reverse (Leetcode Medium)Programming Live with Larry · 4:52 · 371 views
  3. Leetcode Weekly contest 315 - Medium - Sum of Number and Its ReversePrakhar Agrawal · 4:06 · 288 views
  4. Sum of Number and Its Reverse - Leetcode Weekly Contest 315Gopala Krishna Reddy Maguluri · 0:27 · 284 views
  5. 2443. Sum of Number and Its Reverse||Leetcode weekly contest 315 || Code In Description || C++CS warriors · 3:21 · 207 views
  6. LeetCode - 2443. Sum of Number and Its Reverse | Math | Python | JavaOrkhan Gasanov · 7:10 · 149 views
  7. LeetCode 2443 | Sum of Number and Its Reverse 🔥 | Easy C++ Solution + ExplanationManan patel · 8:47 · 102 views
  8. Random Problem Challenge IV - Sum of Number and Its Reverse - LeetCode 2443 - PythonAlpha-Code · 15:28 · 42 views
  9. LeetCode 2443 Solution in C++ | Sum of Number and Reverse | Step-by-Step ExplanationRakshit Raj · 7:39 · 17 views
  10. Sum of Number and Its Reverse | LeetCode | C++ Easy ExplanationHaqueCodes · 4:20 · 17 views

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 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

Approach overview

Problem Overview: Given an integer num, determine whether there exists a non‑negative integer x such that x + reverse(x) = num. The task is simply to test if any candidate number produces the required sum when its digits are reversed.

Approach 1: Brute Force Iteration and Reversal (O(n log n) time, O(1) space)

Iterate through every possible value x from 0 to num. For each candidate, compute its digit reversal and check whether x + reverse(x) equals num. Reversing digits requires repeatedly extracting digits using modulus and division, which costs O(log n) time per number because the number of digits grows logarithmically. This approach is straightforward and relies purely on enumeration combined with basic math operations. Since only a few variables are used during digit reversal, the space complexity stays O(1).

This method works well because the search space is bounded: if x > num, then x + reverse(x) must also exceed num. Therefore testing all values up to num guarantees that every valid candidate is checked.

Approach 2: Optimized Iteration with Early Break (O(n log n) time, O(1) space)

The optimized version keeps the same enumeration strategy but introduces simple pruning during iteration. Since reverse(x) is always non‑negative, once x itself becomes greater than num, the sum x + reverse(x) must exceed num. The loop can therefore safely restrict candidates to the range 0..num and stop immediately if the current candidate cannot possibly form the required sum.

During each iteration, compute the reversed number using digit extraction and check the equation x + reverse(x) == num. If a match appears, return true immediately. If the loop completes without finding a valid candidate, return false. The complexity remains O(n log n) because each iteration still performs a digit reversal, but the early termination avoids unnecessary checks in practice.

Recommended for interviews: The enumeration approach is exactly what most interviewers expect. The brute force version demonstrates that you recognized the bounded search space and know how to implement digit reversal. Adding the early break shows awareness of constraints and small optimizations. Since the problem is primarily about reasoning with numbers rather than advanced data structures, a clean math and enumeration implementation with O(n log n) time and O(1) space is considered the correct solution.

Approach 1: Brute Force Iteration and Reversal

The brute force approach involves iterating over all numbers from 0 to num. For each number, we'll calculate its reverse and check if the sum of the number and its reverse equals num. This approach is simple to understand but may not be the most efficient for larger values of num.

def reverse(n):
    rev = 0
    while n > 0:
        rev = rev * 10 + n % 10
        n //= 10
    return rev


def sum_of_number_and_reverse(num):
    for i in range(num + 1):
        if i + reverse(i) == num:
            return True
    return False


num = 443
print(sum_of_number_and_reverse(num))
public class Main {
    public static int reverse(int n) {
        int rev = 0;
        while (n > 0) {
            rev = rev * 10 + n % 10;
            n /= 10;
        }
        return rev;
    }

    public static boolean sumOfNumberAndReverse(int num) {
        for (int i = 0; i <= num; i++) {
            if (i + reverse(i) == num) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int num = 443;
        System.out.println(sumOfNumberAndReverse(num));
    }
}
#include <iostream>
using namespace std;

int reverse(int n) {
    int rev = 0;
    while (n > 0) {
        rev = rev * 10 + n % 10;
        n /= 10;
    }
    return rev;
}

bool sumOfNumberAndReverse(int num) {
    for (int i = 0; i <= num; i++) {
        if (i + reverse(i) == num) {
            return true;
        }
    }
    return false;
}

int main() {
    int num = 443;
    cout << (sumOfNumberAndReverse(num) ? "true" : "false") << endl;
    return 0;
}
function reverse(n) {
    let rev = 0;
    while (n > 0) {
        rev = rev * 10 + n % 10;
        n = Math.floor(n / 10);
    }
    return rev;
}

function sumOfNumberAndReverse(num) {
    for (let i = 0; i <= num; i++) {
        if (i + reverse(i) === num) {
            return true;
        }
    }
    return false;
}

const num = 443;
console.log(sumOfNumberAndReverse(num));
#include <stdio.h>
#include <stdbool.h>

int reverse(int n) {
    int rev = 0;
    while (n > 0) {
        rev = rev * 10 + n % 10;
        n /= 10;
    }
    return rev;
}

bool sumOfNumberAndReverse(int num) {
    for (int i = 0; i <= num; i++) {
        if (i + reverse(i) == num) {
            return true;
        }
    }
    return false;
}

int main() {
    int num = 443;
    printf("%s\n", sumOfNumberAndReverse(num) ? "true" : "false");
    return 0;
}
using System;

public class MainClass {
    public static int Reverse(int n) {
        int rev = 0;
        while (n > 0) {
            rev = rev * 10 + n % 10;
            n /= 10;
        }
        return rev;
    }

    public static bool SumOfNumberAndReverse(int num) {
        for (int i = 0; i <= num; i++) {
            if (i + Reverse(i) == num) {
                return true;
            }
        }
        return false;
    }

    public static void Main(string[] args) {
        int num = 443;
        Console.WriteLine(SumOfNumberAndReverse(num));
    }
}

This Python solution iterates through integers up to num. For each integer, it calculates its reverse and checks if their sum is equal to num. If a valid pair is found, the function immediately returns True. Otherwise, it returns False.

Complexity

Time Complexity: O(n * d), where n is num and d is the number of digits in n.
Space Complexity: O(1).

Try this approach in the editor

Approach 2: Optimized Iteration with Early Break

To optimize the iteration, we can iteratively consider only numbers whose reverses are significantly smaller. If the reversed number surpasses num/2, the iteration can break early as it's impossible for their sum to yield num from that point onward.

def reverse(n):
    rev = 0
    while n > 0:
        rev = rev * 10 + n % 10
        n //= 10
    return rev


def sum_of_number_and_reverse(num):
    for i in range(num // 2 + 1):
        if i + reverse(i) == num:
            return True
    return False


num = 181
print(sum_of_number_and_reverse(num))
public class Main {
    public static int reverse(int n) {
        int rev = 0;
        while (n > 0) {
            rev = rev * 10 + n % 10;
            n /= 10;
        }
        return rev;
    }

    public static boolean sumOfNumberAndReverse(int num) {
        for (int i = 0; i <= num / 2; i++) {
            if (i + reverse(i) == num) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int num = 181;
        System.out.println(sumOfNumberAndReverse(num));
    }
}
#include <iostream>
using namespace std;

int reverse(int n) {
    int rev = 0;
    while (n > 0) {
        rev = rev * 10 + n % 10;
        n /= 10;
    }
    return rev;
}

bool sumOfNumberAndReverse(int num) {
    for (int i = 0; i <= num / 2; i++) {
        if (i + reverse(i) == num) {
            return true;
        }
    }
    return false;
}

int main() {
    int num = 181;
    cout << (sumOfNumberAndReverse(num) ? "true" : "false") << endl;
    return 0;
}
function reverse(n) {
    let rev = 0;
    while (n > 0) {
        rev = rev * 10 + n % 10;
        n = Math.floor(n / 10);
    }
    return rev;
}

function sumOfNumberAndReverse(num) {
    for (let i = 0; i <= Math.floor(num / 2); i++) {
        if (i + reverse(i) === num) {
            return true;
        }
    }
    return false;
}

const num = 181;
console.log(sumOfNumberAndReverse(num));
#include <stdio.h>
#include <stdbool.h>

int reverse(int n) {
    int rev = 0;
    while (n > 0) {
        rev = rev * 10 + n % 10;
        n /= 10;
    }
    return rev;
}

bool sumOfNumberAndReverse(int num) {
    for (int i = 0; i <= num / 2; i++) {
        if (i + reverse(i) == num) {
            return true;
        }
    }
    return false;
}

int main() {
    int num = 181;
    printf("%s\n", sumOfNumberAndReverse(num) ? "true" : "false");
    return 0;
}
using System;

public class MainClass {
    public static int Reverse(int n) {
        int rev = 0;
        while (n > 0) {
            rev = rev * 10 + n % 10;
            n /= 10;
        }
        return rev;
    }

    public static bool SumOfNumberAndReverse(int num) {
        for (int i = 0; i <= num / 2; i++) {
            if (i + Reverse(i) == num) {
                return true;
            }
        }
        return false;
    }

    public static void Main(string[] args) {
        int num = 181;
        Console.WriteLine(SumOfNumberAndReverse(num));
    }
}

The Python solution includes the early breaking strategy by handling only numbers up to split the exploration at num / 2, focusing on combinations that could realistically form num when summed with their reverse.

Complexity

Time Complexity: O(n / 2 * d).
Space Complexity: O(1).

Try this approach in the editor

Approach 3: Brute Force Enumeration

Enumerate k in the range [0,.., num], and check whether k + reverse(k) equals num.

The time complexity is O(n times log n), where n is the size of num.

function sumOfNumberAndReverse(num: number): boolean {
    for (let i = 0; i <= num; i++) {
        if (i + Number([...(i + '')].reverse().join('')) === num) {
            return true;
        }
    }
    return false;
}
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
}
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
    }
}
Try this approach in the editor

Complexity comparison

ApproachComplexity
Brute Force Iteration and Reversal

Time Complexity: O(n * d), where n is num and d is the number of digits in n.
Space Complexity: O(1).

Optimized Iteration with Early Break

Time Complexity: O(n / 2 * d).
Space Complexity: O(1).

Brute Force Enumeration–

Complexity analysis

ApproachTimeSpaceWhen to Use
Brute Force Iteration and ReversalO(n log n)O(1)Best baseline solution when exploring all candidates directly
Optimized Iteration with Early BreakO(n log n)O(1)Preferred in interviews to avoid unnecessary checks while keeping the logic simple

Frequently asked questions

Is Sum of Number and Its Reverse easy or hard?

The problem is rated Medium on LeetCode because it requires recognizing that the search space is bounded and implementing digit reversal correctly. The logic itself is straightforward once you realize you can simply enumerate possible values.

Sum of Number and Its Reverse Python/Java solution

In Python or Java, iterate from 0 to num, compute the reversed number using a loop that extracts digits, and check whether x + reversed equals num. If a match is found, return true; otherwise continue until the range is exhausted. The implementation uses constant extra space.

How to solve Sum of Number and Its Reverse in O(n)?

Iterate through numbers from 0 to num and compute the reversed digits for each candidate. For each value x, calculate reverse(x) using repeated modulus and division operations, then check whether x + reverse(x) equals num. The loop runs n times, and digit reversal adds a logarithmic factor, giving O(n log n) time overall.

What is the best approach for Sum of Number and Its Reverse?

The standard solution is enumeration. Iterate through every integer x from 0 to num, compute reverse(x), and check if x + reverse(x) equals num. This runs in O(n log n) time because reversing digits takes O(log n) per iteration and uses O(1) extra space.

Is Sum of Number and Its Reverse asked at Google/Amazon/Meta?

This problem represents the type of math and enumeration question commonly used in technical screens. Variations involving digit manipulation or reversing numbers appear in interviews at companies like Google, Amazon, and Meta to test reasoning with numeric transformations.

What data structure is used in Sum of Number and Its Reverse?

No specialized data structure is required. The solution relies on simple integer arithmetic and digit manipulation. Enumeration and mathematical operations are the primary techniques used.

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

The typical solution runs in O(n log n) time. You may test up to n candidates, and reversing the digits of each number costs O(log n) based on the number of digits. Space complexity remains O(1) because only a few variables are used during computation.

Comments

Now write it yourselfClose this page and solve Sum of Number and Its Reverse in the editor, without looking.
Practise Sum of Number and Its Reverse