#3754
Easy Algorithms Concatenate non zero digits and multiply by sum i
Math
55.7% acceptance
Feb 25, 2026
36
0
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn sum_and_multiply(n: i32) -> i64 {
let digits: Vec<i64> = n.to_string().bytes()
.map(|b| (b - b'0') as i64)
.filter(|&d| d != 0)
.collect();
if digits.is_empty() { return 0; }
let x: i64 = digits.iter().fold(0i64, |acc, &d| acc * 10 + d);
let sum: i64 = digits.iter().sum();
x * sum
}
}