Talentd
FleetCode
Make and follow sheets.Sign in

Concatenate Non-Zero Digits and Multiply by Sum I solution and explanation

EasyMath3 min read66.2% acceptanceUpdated 24 Mar 2026
3754. Concatenate Non-Zero Digits and Multiply by Sum I | Leetcode Daily - PythonLeetcode Daily · 4:26 · 763 viewsWatch on YouTube
9 more videos on this problem
  1. 3754. Concatenate Non-Zero Digits and Multiply by Sum I | Leetcode Daily - PythonLeetcode Daily · 4:26 · 763 views
  2. Leetcode 3754 & 3756 Concatenate Non-Zero Digits and Multiply by Sum I & II | Prefix array | O(N+Q)Leet's Code · 30:23 · 248 views
  3. Concatenate Non-Zero Digits and Multiply by Sum I | LeetCode 3754 | Java Code | Developer CoderDeveloper Coder · 8:11 · 235 views
  4. 3754. Concatenate Non-Zero Digits and Multiply by Sum I (Leetcode Easy)Programming Live with Larry · 2:43 · 192 views
  5. Concatenate Non Zero Digits and Multiply by Sum I | LeetCode 3754 - Pythonimpoldev · 3:33 · 149 views
  6. 🔥 LeetCode 3754 | Concatenate Non-Zero Digits and Multiply by Sum I | C++ Solution | EasyCode Hunt · 5:05 · 96 views
  7. Concatenate Non-Zero Digits and Multiply by Sum I Leetcode 3754codewithsitaram · 14:47 · 65 views
  8. LeetCode 3754 | Concatenate Non-Zero Digits and Multiply by Sum I | Easy Math ApproachDebugger · 6:23 · 52 views
  9. 3754. Concatenate Non-Zero Digits and Multiply by Sum I | POTD 07 July 2026 | C++ | LeetcodeTechie On Track · 5:00 · 40 views
  10. Concatenate Non Zero Digits and Multiply by Sum I Leetcode (07 JULY 2026)CODING TRAINER · 6:44 · 35 views

The problem

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).

class Solution:
    def sumAndMultiply(self, n: int) -> int:
        p = 1
        x = s = 0
        while n:
            v = n % 10
            s += v
            if v:
                x += p * v
                p *= 10
            n //= 10
        return x * s
class Solution {
    public long sumAndMultiply(int n) {
        int p = 1;
        int x = 0, s = 0;
        for (; n > 0; n /= 10) {
            int v = n % 10;
            s += v;
            if (v != 0) {
                x += p * v;
                p *= 10;
            }
        }
        return 1L * x * s;
    }
}
class Solution {
public:
    long long sumAndMultiply(int n) {
        int p = 1;
        int x = 0, s = 0;
        for (; n > 0; n /= 10) {
            int v = n % 10;
            s += v;
            if (v != 0) {
                x += p * v;
                p *= 10;
            }
        }
        return 1LL * x * s;
    }
};
function sumAndMultiply(n: number): number {
    let p = 1;
    let x = 0;
    let s = 0;

    while (n > 0) {
        const v = n % 10;
        s += v;
        if (v !== 0) {
            x += p * v;
            p *= 10;
        }
        n = Math.floor(n / 10);
    }

    return x * s;
}
func sumAndMultiply(n int) int64 {
	p := 1
	x := 0
	s := 0
	for n > 0 {
		v := n % 10
		s += v
		if v != 0 {
			x += p * v
			p *= 10
		}
		n /= 10
	}
	return int64(x) * int64(s)
}
Try this approach in the editor

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

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.

Comments

Now write it yourselfClose this page and solve Concatenate Non-Zero Digits and Multiply by Sum I in the editor, without looking.
Practise Concatenate Non-Zero Digits and Multiply by Sum I