Concatenate Non-Zero Digits and Multiply by Sum I - Solution & Explanation
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 = 1andsum = 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
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Arithmetic Simulation | O(n) | O(1) | Preferred in interviews; efficient digit manipulation without extra memory |
| String-Based Simulation | O(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 - Python • Leetcode Daily • 763 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 Python/Java solution
How to solve Concatenate Non-Zero Digits and Multiply by Sum I in O(n)?
What is the best approach for Concatenate Non-Zero Digits and Multiply by Sum I?
Is Concatenate Non-Zero Digits and Multiply by Sum I asked at Google/Amazon/Meta?
What data structure is used in Concatenate Non-Zero Digits and Multiply by Sum I?
What is the time complexity of Concatenate Non-Zero Digits and Multiply by Sum I?
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 FleetCodeTable of Contents
Practice this problem
Open in Editor