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:
x = 1234.sum = 1 + 2 + 3 + 4 = 10.x * sum = 1234 * 10 = 12340.Example 2:
Input: n = 1000
Output: 1
Explanation:
x = 1 and sum = 1.x * sum = 1 * 1 = 1.
Constraints:
0 <= n <= 109Problem 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.
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).
Python
Java
C++
Go
TypeScript
| 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 |
Leetcode Weekly Contest 477 Q1. Concatenate Non-Zero Digits and Multiply by Sum I #python #dsa • ADevOpsBeginner • 237 views views
Watch 6 more video solutions →Practice Concatenate Non-Zero Digits and Multiply by Sum I with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor