Reverse Integer
Reverse Integer
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2³¹, 2³¹ - 1] (i.e., [-2147483648, 2147483647]), return 0. Your solve(...) function receives these inputs directly as ready-made arguments — no input reading or parsing needed. Just compute and return the answer. Example: x=123 → 321 Example: x=-123 → -321 Example: x=120 → 21 (trailing zero is dropped) Example: x=1534236469 → 0 (reversal overflows)
Input format — only if your code reads from stdin
Input format (stdin): - A single integer x Output: the reversed integer, or 0 if it overflows 32-bit signed range.
Approach
Extract digits one by one using x % 10 and build the reversed number by multiplying current result by 10. Before each multiplication/addition, check if the result would overflow the 32-bit signed range.
Input
Output
- •
2³¹ ≤ x ≤ 2³¹ - 1
More Math problems
Browse all coding problems →Press Run to test your custom input, or Submit to judge against all test cases.
Sign in to continue
Create a free account to run code, submit solutions and track progress.
- Free forever — no credit card
- 10+ languages with live execution
- Points, streaks & leaderboards