Skip to main content

Concatenate Non-Zero Digits and Multiply by Sum I - Solution & Explanation

EasyMath5 min readAsked at: Amazon, Microsoft, Google
Practice this problem

Problem Statement

You are given an integer n.

Form a new integer x by concatenating all the non-zero digits of n in their original order. If there are no non-zero digits, x = 0.

Let sum be the sum of digits in x.

Return an integer representing the value of x * sum.

 

Example 1:

Input: n = 10203004

Output: 12340

Explanation:

  • The non-zero digits are 1, 2, 3, and 4. Thus, x = 1234.
  • The sum of digits is sum = 1 + 2 + 3 + 4 = 10.
  • Therefore, the answer is x * sum = 1234 * 10 = 12340.

Example 2:

Input: n = 1000

Output: 1

Explanation:

  • The non-zero digit is 1, so x = 1 and sum = 1.
  • Therefore, the answer is x * sum = 1 * 1 = 1.

 

Constraints:

  • 0 <= n <= 109

Approach Overview

Problem Overview: You process the digits of a number and build a new value from them. All non-zero digits are concatenated to form a new integer, while the sum of all digits is calculated separately. The final result is the concatenated number multiplied by the digit sum.

Approach 1: Simulation Using Digit Traversal (O(n) time, O(1) space)

Iterate through every digit of the number once. Maintain two variables: one for the running digitSum and another for the concatenated value formed by non-zero digits. When you encounter a digit, add it to the sum. If the digit is not zero, append it to the result using concat = concat * 10 + digit. This works because multiplying by 10 shifts the previous digits left and creates space for the new one. After processing all digits, return concat * digitSum.

This direct simulation is efficient because each digit is processed exactly once. No additional data structures are needed, and all operations are constant time per digit. Problems like this commonly appear under Math and Simulation patterns where you model the exact steps described in the prompt.

Approach 2: String-Based Simulation (O(n) time, O(n) space)

Convert the number to a string and iterate through its characters. Compute the sum of digits while simultaneously building a string that contains only the non-zero digits. After the loop, convert the filtered string back to an integer and multiply it by the digit sum. This approach is straightforward and often easier to implement during interviews when readability matters.

The downside is the extra memory used to build the intermediate string and the conversion overhead. Still, the complexity remains linear in the number of digits, making it perfectly acceptable for typical constraints.

Recommended for interviews: The arithmetic simulation approach is usually preferred. It shows that you understand digit manipulation without relying on string conversions. Mentioning both solutions is useful: the string version demonstrates quick problem translation, while the arithmetic version highlights stronger control over math operations and low-level implementation details.

Solution

We can simulate the required operation by processing the number digit by digit. While processing each digit, we concatenate non-zero digits to form a new integer x and calculate the digit sum s. Finally, we return x times s.

The time complexity is O(log n) and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Arithmetic SimulationO(n)O(1)Preferred in interviews; efficient digit manipulation without extra memory
String-Based SimulationO(n)O(n)Useful for quick implementation and readability when string handling is allowed

Video Solution

3754. Concatenate Non-Zero Digits and Multiply by Sum I | Leetcode Daily - PythonLeetcode Daily763 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Concatenate Non-Zero Digits and Multiply by Sum I easy or hard?
Concatenate Non-Zero Digits and Multiply by Sum I is categorized as an Easy problem. It mainly tests basic digit processing, arithmetic manipulation, and careful implementation rather than advanced algorithms.
Concatenate Non-Zero Digits and Multiply by Sum I Python/Java solution
The solution in Python or Java typically loops through each digit, updates the digit sum, and appends non-zero digits to a running integer using multiplication by 10. The final step multiplies the constructed number by the total digit sum. The same logic works in C++, Go, and TypeScript.
How to solve Concatenate Non-Zero Digits and Multiply by Sum I in O(n)?
Traverse the digits of the number once. Maintain two variables: the running sum of digits and a concatenated value formed only from non-zero digits using concat = concat * 10 + digit. After processing all digits, multiply the concatenated number by the digit sum. This ensures linear time with constant extra space.
What is the best approach for Concatenate Non-Zero Digits and Multiply by Sum I?
The best approach is a single-pass simulation over the digits. Track the digit sum while building another number that concatenates only non-zero digits using arithmetic operations. This solution runs in O(n) time with O(1) space, where n is the number of digits.
Is Concatenate Non-Zero Digits and Multiply by Sum I asked at Google/Amazon/Meta?
Problems like this appear in coding interviews as warm-up or screening questions focused on math and simulation. Large companies such as Google, Amazon, and Meta frequently include similar digit-manipulation tasks to test careful implementation and edge-case handling.
What data structure is used in Concatenate Non-Zero Digits and Multiply by Sum I?
No advanced data structure is required. The solution relies on simple arithmetic operations and integer variables to track the digit sum and the concatenated result. Some implementations may temporarily use strings, but the optimal version works entirely with integers.
What is the time complexity of Concatenate Non-Zero Digits and Multiply by Sum I?
The time complexity is O(n) because each digit of the number is processed exactly once. The space complexity can be O(1) when using arithmetic concatenation, or O(n) if a temporary string is used to collect non-zero digits.

Ready to solve this problem?

Practice Concatenate Non-Zero Digits and Multiply by Sum I with our built-in code editor and test cases.

Practice on FleetCode