Skip to content

Commit 3102782

Browse files
committed
Added task 3754
1 parent eac56f3 commit 3102782

3 files changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g3701_3800.s3754_concatenate_non_zero_digits_and_multiply_by_sum_i;
2+
3+
// #Easy #Math #Weekly_Contest_477 #2026_04_25_Time_1_ms_(100.00%)_Space_42.50_MB_(73.85%)
4+
5+
public class Solution {
6+
public long sumAndMultiply(int n) {
7+
int newNum = 0;
8+
int y = Math.abs(n);
9+
while (y > 0) {
10+
int rem = y % 10;
11+
if (rem != 0) {
12+
newNum = newNum * 10 + rem;
13+
}
14+
y /= 10;
15+
}
16+
int temp = 0;
17+
while (newNum > 0) {
18+
int rem = newNum % 10;
19+
temp = temp * 10 + rem;
20+
newNum /= 10;
21+
}
22+
int x = temp;
23+
long sum = 0;
24+
while (temp > 0) {
25+
int rem = temp % 10;
26+
sum += rem;
27+
temp /= 10;
28+
}
29+
return sum * x;
30+
}
31+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
3754\. Concatenate Non-Zero Digits and Multiply by Sum I
2+
3+
Easy
4+
5+
You are given an integer `n`.
6+
7+
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`.
8+
9+
Let `sum` be the **sum of digits** in `x`.
10+
11+
Return an integer representing the value of `x * sum`.
12+
13+
**Example 1:**
14+
15+
**Input:** n = 10203004
16+
17+
**Output:** 12340
18+
19+
**Explanation:**
20+
21+
* The non-zero digits are 1, 2, 3, and 4. Thus, `x = 1234`.
22+
* The sum of digits is `sum = 1 + 2 + 3 + 4 = 10`.
23+
* Therefore, the answer is `x * sum = 1234 * 10 = 12340`.
24+
25+
**Example 2:**
26+
27+
**Input:** n = 1000
28+
29+
**Output:** 1
30+
31+
**Explanation:**
32+
33+
* The non-zero digit is 1, so `x = 1` and `sum = 1`.
34+
* Therefore, the answer is `x * sum = 1 * 1 = 1`.
35+
36+
**Constraints:**
37+
38+
* <code>0 <= n <= 10<sup>9</sup></code>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3701_3800.s3754_concatenate_non_zero_digits_and_multiply_by_sum_i;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void sumAndMultiply() {
11+
assertThat(new Solution().sumAndMultiply(10203004), equalTo(12340L));
12+
}
13+
14+
@Test
15+
void sumAndMultiply2() {
16+
assertThat(new Solution().sumAndMultiply(1000), equalTo(1L));
17+
}
18+
}

0 commit comments

Comments
 (0)