Talentd
FleetCode
Make and follow sheets.Sign in

Remove Zeros in Decimal Representation solution and explanation

EasyMathSimulation3 min read75.7% acceptanceUpdated 24 Mar 2026
3726. Remove Zeros in Decimal Representation (Leetcode Easy)Programming Live with Larry · 1:45 · 441 viewsWatch on YouTube
8 more videos on this problem
  1. 3726. Remove Zeros in Decimal Representation (Leetcode Easy)Programming Live with Larry · 1:45 · 441 views
  2. Remove Zeros in Decimal Representation | Weekly Contest 473 | Java Code | Developer CoderDeveloper Coder · 7:18 · 236 views
  3. Leetcode 3726 | Remove Zeros in Decimal Representation| Detailed explanation | Weekly Contest 473Samrat Bhardwaj · 4:25 · 235 views
  4. Leetcode Weekly Contest 473 Q1. Remove Zeros in Decimal Representation #python #dsaADevOpsEngineer · 1:43 · 154 views
  5. Q1 - Remove Zeros in Decimal Representation - C# SolutionAxoryn · 0:43 · 42 views
  6. Remove Zeros in Decimal Representation | 1 Best Approach | Leetcode 3726| Mayank PandeyMayank Pandey · 7:50 · 37 views
  7. 3726. Remove Zeros in Decimal Representation #leetcode #coding #programmingCodeTalk18 · 5:21 · 11 views
  8. Leetcode 3/909 (Easy) - Remove Zeros in Decimal RepresentationTomas Guzman · 2:36 · 4 views
  9. LeetCode | Solved in 2:50 mins | EP18 | 3726. Remove Zeros in Decimal RepresentationEduardo Nakanishi · 2:50 · 3 views

The problem

You are given a positive integer n.

Return the integer obtained by removing all zeros from the decimal representation of n.

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:

  • 1 <= n <= 1015

Approach overview

Problem Overview: You are given an integer n. The task is simple: remove every 0 digit from its decimal representation and return the resulting number. The relative order of the remaining digits must stay the same.

Approach 1: String Simulation (O(d) time, O(d) space)

Convert the number to a string and iterate through each character. Skip characters equal to '0' and append the rest to a new string or buffer. After the scan, convert the filtered string back to an integer. The key idea is that string traversal makes digit filtering trivial and avoids manual digit manipulation.

This approach is easy to implement and highly readable. Every digit is processed once, giving O(d) time where d is the number of digits. Extra space is O(d) for the temporary string. This solution relies on straightforward simulation of the digit removal process and is usually the fastest way to write correct code in interviews.

Approach 2: Mathematical Digit Construction (O(d) time, O(1) space)

Instead of converting to a string, process the number using arithmetic. Repeatedly extract digits with n % 10, skip the digit if it equals 0, and rebuild the result using positional multiplication. Because digits are extracted from right to left, you either build the number in reverse and flip it later, or track a multiplier to place digits correctly.

This method avoids additional memory and works entirely with integer operations. The loop runs once per digit, so the time complexity remains O(d) and the extra space is O(1). This solution highlights comfort with math operations and manual digit manipulation, which is common in problems involving decimal representation.

Recommended for interviews: Both approaches run in linear time relative to the number of digits. The string-based simulation is typically expected because it is concise and less error-prone. Showing the mathematical version demonstrates deeper understanding of number manipulation and simulation techniques, which interviewers often appreciate for follow-up discussion.

Solution

We start from the lowest digit of n and check each digit one by one. If the digit is not zero, we add it to the result. We also need a variable to keep track of the current digit position in order to correctly construct the final integer.

Specifically, we can use a variable k to represent the current digit position, then check each digit from the lowest to the highest. If the digit is not zero, we multiply it by k and add it to the result, and then multiply k by 10 for the next digit.

In the end, we obtain an integer without any zeros.

The time complexity is O(d), where d is the number of digits in n. The space complexity is O(1).

class Solution:
    def removeZeros(self, n: int) -> int:
        k = 1
        ans = 0
        while n:
            x = n % 10
            if x:
                ans = k * x + ans
                k *= 10
            n //= 10
        return ans
class Solution {
    public long removeZeros(long n) {
        long k = 1;
        long ans = 0;
        while (n > 0) {
            long x = n % 10;
            if (x > 0) {
                ans = k * x + ans;
                k *= 10;
            }
            n /= 10;
        }
        return ans;
    }
}
class Solution {
public:
    long long removeZeros(long long n) {
        long long k = 1;
        long long ans = 0;
        while (n > 0) {
            long x = n % 10;
            if (x > 0) {
                ans = k * x + ans;
                k *= 10;
            }
            n /= 10;
        }
        return ans;
    }
};
function removeZeros(n: number): number {
    let k = 1;
    let ans = 0;
    while (n) {
        const x = n % 10;
        if (x) {
            ans = k * x + ans;
            k *= 10;
        }
        n = Math.floor(n / 10);
    }
    return ans;
}
func removeZeros(n int64) (ans int64) {
	k := int64(1)
	for n > 0 {
		x := n % 10
		if x > 0 {
			ans = k*x + ans
			k *= 10
		}
		n /= 10
	}
	return
}
Try this approach in the editor

Complexity analysis

ApproachTimeSpaceWhen to Use
String SimulationO(d)O(d)Best for clarity and quick implementation using string filtering
Mathematical Digit ConstructionO(d)O(1)When avoiding extra memory or practicing digit manipulation

Frequently asked questions

Is Remove Zeros in Decimal Representation easy or hard?

Remove Zeros in Decimal Representation is generally classified as an Easy problem. The logic involves basic digit processing using either string traversal or simple arithmetic operations, making it approachable for beginners practicing simulation or math-based problems.

Remove Zeros in Decimal Representation Python/Java solution

In Python or Java, the typical solution converts the number to a string, iterates through characters, skips '0', and reconstructs the result. This approach takes O(d) time and is concise in both languages using built-in string operations.

How to solve Remove Zeros in Decimal Representation in O(n)?

Process the digits sequentially and skip zeros. One method converts the number to a string and filters characters. Another uses repeated modulo and division operations (n % 10 and n / 10) to extract digits and rebuild the result. Both approaches scan digits once, giving O(d) time complexity.

What is the best approach for Remove Zeros in Decimal Representation?

The most practical approach is string-based simulation. Convert the integer to a string, iterate through each character, skip '0', and rebuild the number from the remaining digits. This runs in O(d) time where d is the number of digits and is simple to implement in most languages.

Is Remove Zeros in Decimal Representation asked at Google/Amazon/Meta?

Problems involving digit manipulation and simulation frequently appear in interviews at large tech companies. While this exact problem may vary, the underlying techniques—string processing and mathematical digit extraction—are common in coding interviews at companies like Google, Amazon, and Meta.

What data structure is used in Remove Zeros in Decimal Representation?

The problem mainly uses strings or simple integer variables. The string approach stores digits temporarily while filtering out '0'. The mathematical approach uses only integer arithmetic, making it a constant-space solution without additional data structures.

What is the time complexity of Remove Zeros in Decimal Representation?

The optimal solution runs in O(d) time, where d is the number of digits in the integer. Each digit is processed exactly once while filtering out zeros. Space complexity is O(d) for the string approach or O(1) if you rebuild the number using arithmetic operations.

Comments

Now write it yourselfClose this page and solve Remove Zeros in Decimal Representation in the editor, without looking.
Practise Remove Zeros in Decimal Representation