Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# 3754. Concatenate Non-Zero Digits and Multiply by Sum I

[LeetCode Link](https://leetcode.com/problems/concatenate-non-zero-digits-and-multiply-by-sum-i/)

Difficulty: Easy
Topics: Math

Acceptance Rate: 60.8%

## Hints

### Hint 1

This is a digit-manipulation problem. Think about how you can inspect the individual
digits of an integer one at a time. Two classic tools come to mind: converting the
number to a string, or repeatedly using `% 10` and `/ 10`. Either can walk the digits.

### Hint 2

You need to build a new number `x` from only the **non-zero** digits, keeping their
original left-to-right order. As you keep each non-zero digit, you also need its
running sum. Notice that both `x` and `sum` can be accumulated in a single pass — you
do not need to store the digits separately first.

### Hint 3

If you scan the digits from most-significant to least-significant, you can grow `x`
with the rule `x = x*10 + digit` for every non-zero digit, and simultaneously add that
digit to `sum`. Skip any zero digit entirely. If no non-zero digit is ever seen, `x`
stays `0` (which also makes `sum` `0`), so the answer is naturally `0`. Finally return
`x * sum`.

## Approach

The task decomposes into two coupled accumulations over the digits of `n`:

1. **Build `x`** — the integer formed by the non-zero digits in original order.
2. **Compute `sum`** — the sum of the digits of `x` (equivalently, the sum of the
non-zero digits of `n`, since zeros contribute nothing).

The cleanest way to preserve original order is to process digits from most significant
to least significant. The simplest way to get that order is to turn `n` into its decimal
string and iterate left to right. For each character:

- Convert it to its digit value `d`.
- If `d != 0`, extend `x` with `x = x*10 + d` and add `d` to `sum`.
- If `d == 0`, ignore it.

Walk through Example 1 with `n = 10203004`:

| digit | non-zero? | x after | sum after |
|-------|-----------|---------|-----------|
| 1 | yes | 1 | 1 |
| 0 | no | 1 | 1 |
| 2 | yes | 12 | 3 |
| 0 | no | 12 | 3 |
| 3 | yes | 123 | 6 |
| 0 | no | 123 | 6 |
| 0 | no | 123 | 6 |
| 4 | yes | 1234 | 10 |

Result: `x * sum = 1234 * 10 = 12340`. ✅

For Example 2, `n = 1000`, only the leading `1` survives, giving `x = 1`, `sum = 1`,
and `1 * 1 = 1`. ✅

An alternative is a pure-math approach using `% 10` / `/ 10`, but that yields digits
from least significant first, so you would have to reverse or use place multipliers to
rebuild `x` in the correct order. The string scan avoids that bookkeeping and stays
easy to read, which is why the reference solution uses it.

Because `0 <= n <= 10^9`, `n` has at most 10 digits, so `x` fits comfortably in a
64-bit (and even a 32-bit) integer, and `x * sum` cannot overflow Go's `int`.

## Complexity Analysis

Time Complexity: O(d) where d is the number of digits in `n` (at most 10) — effectively O(1).
Space Complexity: O(d) for the string representation (O(1) if using the pure-math variant).

## Edge Cases

- **`n = 0`**: There are no non-zero digits, so `x = 0`, `sum = 0`, and the answer is
`0`. The single `0` digit is skipped, leaving the accumulators at their initial `0`.
- **No non-zero digits beyond a leading value that is itself zero**: Same as above —
any all-zero input collapses to `0`.
- **Trailing/interior zeros (`n = 1000`, `n = 10203004`)**: Zeros must be dropped, not
treated as placeholders. Building `x` only on non-zero digits handles this correctly.
- **Single non-zero digit (`n = 7`)**: `x = 7`, `sum = 7`, answer `49`. Confirms the
loop works with one iteration.
- **Maximum input (`n = 10^9 = 1000000000`)**: Only the leading `1` is non-zero, so
`x = 1`, `sum = 1`, answer `1`. Confirms no overflow at the constraint boundary.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
number: "3754"
frontend_id: "3754"
title: "Concatenate Non-Zero Digits and Multiply by Sum I"
slug: "concatenate-non-zero-digits-and-multiply-by-sum-i"
difficulty: "Easy"
topics:
- "Math"
acceptance_rate: 6082.1
is_premium: false
created_at: "2026-07-07T04:30:54.601612+00:00"
fetched_at: "2026-07-07T04:30:54.601612+00:00"
link: "https://leetcode.com/problems/concatenate-non-zero-digits-and-multiply-by-sum-i/"
date: "2026-07-07"
---

# 3754. Concatenate Non-Zero Digits and Multiply by Sum I

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`
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import "strconv"

// Approach: Scan the decimal digits of n from most significant to least
// significant (by iterating over its string form). For each non-zero digit d,
// grow the concatenated value with x = x*10 + d and add d to sum. Zero digits
// are skipped. If there are no non-zero digits, x and sum both remain 0, so the
// answer is naturally 0. Finally return x * sum.
//
// n has at most 10 digits (0 <= n <= 10^9), so x and x*sum fit in an int.
func concatenateNonZeroDigits(n int) int {
x, sum := 0, 0
for _, c := range strconv.Itoa(n) {
d := int(c - '0')
if d != 0 {
x = x*10 + d
sum += d
}
}
return x * sum
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import "testing"

func TestConcatenateNonZeroDigits(t *testing.T) {
tests := []struct {
name string
n int
expected int
}{
{"example 1: interior zeros, x=1234 sum=10", 10203004, 12340},
{"example 2: trailing zeros, x=1 sum=1", 1000, 1},
{"edge case: n=0 has no non-zero digits", 0, 0},
{"edge case: single non-zero digit", 7, 49},
{"edge case: max input 10^9 keeps only leading 1", 1000000000, 1},
{"edge case: no zeros present", 123, 738},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := concatenateNonZeroDigits(tt.n)
if result != tt.expected {
t.Errorf("concatenateNonZeroDigits(%d) = %d, want %d", tt.n, result, tt.expected)
}
})
}
}