Easy 0 solved 0% acceptance

Palindrome Number

Check whether a number reads the same backwards.

What you'll learn

  • Reverse a number digit by digit
  • Compare the reversed value with the original
  • Handle negative numbers and trailing zeros

Problem

Decide whether the decimal digits of n read the same forwards and backwards. Negative numbers are never palindromes because of the minus sign.

Implement is_palindrome_number(n).

Input

n: The number to check.

Output

Return true for a palindromic number, otherwise false.

Constraints

-10¹⁸ ≤ n ≤ 10¹⁸

Examples

Example 1
Input:
n = 12321

Expected output:
expected = True
Input:
long long n = 12321;

Expected output:
bool expected = true;
Input:
long n = 12321;

Expected output:
bool expected = true;

Explanation

12321 reversed is 12321.

Example 2
Input:
n = -121

Expected output:
expected = False
Input:
long long n = -121;

Expected output:
bool expected = false;
Input:
long n = -121;

Expected output:
bool expected = false;

Explanation

The minus sign makes "-121" differ from "121-".

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

A negative number cannot be a palindrome because of the minus sign.

Hint 2

Compare the number’s digits from both ends rather than converting unnecessarily.

Hint 3

Single-digit numbers are always palindromes.

Expected complexity

O(log n) time, O(1) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.