Skip to main content

Concatenate Non-Zero Digits and Multiply by Sum I - Video Solutions

EasyMath

3754. Concatenate Non-Zero Digits and Multiply by Sum I | Leetcode Daily - Python

Leetcode Daily
4:26763 views
10 video solutions available

Concatenate Non-Zero Digits and Multiply by Sum I - Video Solution

Watch 10 video solutions for Concatenate Non-Zero Digits and Multiply by Sum I, a easy level problem involving Math. This walkthrough by Leetcode Daily has 763 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

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
Read full problem with examples

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.

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