Skip to content

Commit 8dcc90c

Browse files
authored
Create 1317-convert-integer-to-the-sum-of-two-no-zero-integers.java
1 parent f0d6ad5 commit 8dcc90c

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Problem:
3+
* https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/
4+
*
5+
* Given an integer n, return two integers a and b such that:
6+
* - a + b == n
7+
* - Neither a nor b contains the digit zero in their decimal representation.
8+
* You can return any valid pair [a, b].
9+
*
10+
* Example:
11+
* Input: n = 11
12+
* Output: [2, 9]
13+
* Explanation: 2 + 9 = 11 and neither 2 nor 9 contains zero.
14+
*
15+
* Constraints:
16+
* - 2 <= n <= 10^4
17+
*
18+
* Approach:
19+
* We iterate from i = 1 to n - 1. For each i, we check if both i and (n - i)
20+
* do not contain any zero digit by using the containsZero helper method.
21+
* As soon as we find such a pair, we return it.
22+
* The containsZero method checks if a number has the digit 0 by iterating
23+
* through its digits using modulo and division.
24+
*
25+
* Time Complexity:
26+
* O(n * log(n)) — In the worst case, we iterate up to n and for each number,
27+
* checking if it contains zero takes O(log n) time (based on the number of digits).
28+
*
29+
* Space Complexity:
30+
* O(1) — We only use a constant amount of extra space for variables.
31+
*/
32+
class Solution {
33+
public int[] getNoZeroIntegers(int n) {
34+
for (int i = 1; i < n; i++) {
35+
if (!containsZero(i) && !containsZero(n - i)) {
36+
return new int[] { i, n - i };
37+
}
38+
}
39+
return new int[] { 1, n - 1 };
40+
}
41+
42+
private boolean containsZero(int num) {
43+
while (num > 0) {
44+
int digit = num % 10;
45+
if (digit == 0) {
46+
return true;
47+
}
48+
num = num / 10;
49+
}
50+
return false;
51+
}
52+
}

0 commit comments

Comments
 (0)