class Solution:
def getNoZeroIntegers(self, n: int) -> [int]:
def has_zero(num):
return '0' in str(num)
for a in range(1, n):
b = n - a
if not has_zero(a) and not has_zero(b):
return [a, b]
class Solution {
public:
vector<int> getNoZeroIntegers(int n) {
auto hasZero = [](int num) {
while (num) {
if (num % 10 == 0) return true;
num /= 10;
}
return false;
};
for (int a = 1; a < n; ++a) {
int b = n - a;
if (!hasZero(a) && !hasZero(b))
return {a, b};
}
return {};
}
};
class Solution {
public int[] getNoZeroIntegers(int n) {
for (int a = 1; a < n; a++) {
int b = n - a;
if (!hasZero(a) && !hasZero(b)) {
return new int[]{a, b};
}
}
return new int[0];
}
private boolean hasZero(int num) {
while (num > 0) {
if (num % 10 == 0) return true;
num /= 10;
}
return false;
}
}
var getNoZeroIntegers = function(n) {
function hasZero(num) {
return num.toString().includes('0');
}
for (let a = 1; a < n; a++) {
let b = n - a;
if (!hasZero(a) && !hasZero(b)) {
return [a, b];
}
}
};
Given an integer n
, your task is to find two positive integers a
and b
such that:
a + b = n
a
nor b
contains the digit zero in its decimal representation (i.e., both are "no-zero integers")[a, b]
that satisfies these conditions
You can assume that at least one valid solution exists for every input n >= 2
. The returned pair should be in any order, and you do not need to consider uniqueness or all possible solutions—just one valid answer.
The problem asks us to split a given number into two positive parts, ensuring neither part contains the digit zero. The most direct way is to try every possible split (a
from 1 up to n-1
), check if both numbers have no zeros, and return the first valid pair.
At first glance, this seems like a brute-force problem—simply check all possibilities. However, since numbers containing zero are relatively rare compared to all numbers in the range, we can expect to find a solution quickly for any reasonable input. There's no need for a more complex or optimized algorithm.
If you imagine the numbers as tickets, and only some tickets are "bad" (contain zero), you just keep drawing tickets until you get two "good" ones that add up to the target.
a
from 1 to n-1
. For each a
, calculate b = n - a
.
a
and b
, check if their decimal representation contains the digit zero. This can be done by converting the number to a string and checking for '0', or by repeatedly dividing by 10 and checking each digit.
This approach is simple and effective because:
n
.n
in practice.
Let's say n = 11
.
a = 1
, b = 10
: 10
contains a zero, so skip.a = 2
, b = 9
: Both 2
and 9
have no zeros. This is a valid pair!
The algorithm returns [2, 9]
. If a = 3
, b = 8
is also valid, but since we return the first pair found, [2, 9]
is our answer.
For a larger n
, say n = 101
:
a = 1
, b = 100
(100 contains zero)a = 2
, b = 99
(both are valid!)
Again, [2, 99]
is returned.
n-1
pairs in the worst case.O(\log n)
(number of digits in n
).O(n \log n)
.
O(1)
(constant), since we only use a few variables.
To solve the "Convert Integer to the Sum of Two No-Zero Integers" problem, we simply try each possible split of n
into two positive integers and check if both numbers avoid the digit zero. As soon as we find a valid pair, we return it. This direct approach is both simple and efficient, leveraging the fact that valid pairs are common. The elegance of the solution lies in its straightforwardness and the use of basic number and string operations.