commit 6d3580c81bf635ef33ce4e993c9e936866368813
parent b60a0dabdad37bb60ebd75cd48ac2a2174ba696c
Author: Jack Sangdahl <jack@pngu.org>
Date: Tue, 7 Jul 2026 13:49:56 -0600
3754. concatenate non-zero digits and multiply by sum 1
Diffstat:
2 files changed, 49 insertions(+), 0 deletions(-)
diff --git a/concat_non_zero_digit_mult_sum1.c b/concat_non_zero_digit_mult_sum1.c
@@ -0,0 +1,30 @@
+// 3754. Concatenate Non-Zero Digits and Multiply by Sum I
+#include "leetcode.h"
+
+/*
+ * you are given an integer 'n'. from 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'.
+ */
+
+long long sumAndMultiply(int n) {
+ long long x = 0, s = 0, m = 1;
+ while (n) {
+ x = (n % 10) * m + x;
+ if (n % 10)
+ m *= 10;
+ s += n % 10;
+ n /= 10;
+ }
+ return x * s;
+}
+
+int main() {
+ long long r1 = sumAndMultiply(10203004);
+ long long r2 = sumAndMultiply(1000);
+ printf("%lld\n", r1);
+ assert(r1 == 12340);
+ printf("%lld\n", r2);
+ assert(r2 == 1);
+}
diff --git a/concat_non_zero_digit_mult_sum1.py b/concat_non_zero_digit_mult_sum1.py
@@ -0,0 +1,19 @@
+# 3754. Concatenate Non-Zero Digits and Multiply by Sum I
+
+"""
+you are given an integer 'n'. from 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'.
+"""
+
+
+class Solution:
+ def sumAndMultiply(self, n: int) -> int:
+ return n and int(x := str(n).replace("0", "")) * sum(map(int, x))
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.sumAndMultiply(n=10203004))
+ print(obj.sumAndMultiply(n=1000))