Back to blog
Jan 15, 2026
3 min read

Remove Zeros in Decimal Representation

Given an integer n, return the integer obtained by removing all trailing zeros from its decimal representation.

Difficulty: Easy | Acceptance: 76.40% | Paid: No Topics: Math, Simulation

Given an integer n, return the integer obtained by removing all trailing zeros from the decimal representation of n.

Examples

Example 1

Input:

n = 1020030

Output:

123

Explanation: After removing all zeros from 1020030, we get 123.

Example 2

Input:

n = 1

Output:

1

Explanation: 1 has no zero in its decimal representation. Therefore, the answer is 1.

Constraints

0 <= n <= 10^9

String Manipulation

Intuition Convert the integer to a string to easily access and remove characters from the end of the sequence.

Steps

  • Convert the integer n to its string representation.
  • Iterate from the end of the string and remove characters while they are ‘0’.
  • If the resulting string is empty (which happens if input was 0), return 0.
  • Otherwise, convert the modified string back to an integer and return it.
python
class Solution:
    def removeZeros(self, n: int) -&gt; int:
        s = str(n)
        s = s.rstrip('0')
        return int(s) if s else 0

Complexity

  • Time: O(log n) - The number of digits is proportional to log₁₀(n).
  • Space: O(log n) - To store the string representation.
  • Notes: Simple to implement but uses extra space for the string.

Mathematical Iteration

Intuition Use modulo and division operations to mathematically remove trailing zeros without converting to a string.

Steps

  • While n is greater than 0 and the last digit (n % 10) is 0, divide n by 10.
  • Return the resulting n.
python
class Solution:
    def removeZeros(self, n: int) -&gt; int:
        while n &gt; 0 and n % 10 == 0:
            n //= 10
        return n

Complexity

  • Time: O(log n) - We iterate once per digit.
  • Space: O(1) - Constant extra space used.
  • Notes: Optimal space complexity and avoids string conversion overhead.