No-Zero integer is a positive integer that does not contain any 0
in its decimal representation.
Given an integer n
, return a list of two integers [a, b]
where:
a
and b
are No-Zero integers.a + b = n
The test cases are generated so that there is at least one valid solution. If there are many valid solutions, you can return any of them.
Example 1:
Input: n = 2 Output: [1,1] Explanation: Let a = 1 and b = 1. Both a and b are no-zero integers, and a + b = 2 = n.
Example 2:
Input: n = 11 Output: [2,9] Explanation: Let a = 2 and b = 9. Both a and b are no-zero integers, and a + b = 9 = n. Note that there are other valid answers as [8, 3] that can be accepted.
Constraints:
2 <= n <= 104
program main
implicit none
integer :: n, a, b
n = 2
call solve(n, a, b)
print '(I0, " ", I0)', a, b
n = 11
call solve(n, a, b)
print '(I0, " ", I0)', a, b
contains
subroutine solve(n, a, b)
implicit none
integer, intent(in) :: n
integer, intent(out) :: a, b
if (mod(n, 2) == 0) then
a = n / 2
b = n - a
else
a = (n - 1) / 2
b = n - a
end if
end subroutine solve
end program main
1 1 5 6
module no_zero_integers
implicit none
contains
function solve(n) result(a_b)
integer, intent(in) :: n
integer :: a, b, a_b(2)
a = 1
b = n - a
if (a == 0 .or. b == 0) then
a = 1
b = n - a
end if
a_b(1) = a
a_b(2) = b
end function solve
end module no_zero_integers
program main
use no_zero_integers
implicit none
integer :: n
integer :: a_b(2)
n = 2
a_b = solve(n)
write (*,*) a_b
n = 11
a_b = solve(n)
write (*,*) a_b
end program main
1 1 1 10
def no_zero_integers(n: int):
for a in range(1, n):
if '0' not in str(a) and '0' not in str(n - a):
return [a, n - a]
return [0, 0] # never gets here
We will write a function that receives an integer n
as the input. The algorithm then iterates over all integers from 1
to n-1
as a candidate for value a
in the result [a, b]
. For each candidate a
, we check if both a
and n-a
(which would be b
) are No-Zero integers by ensuring that neither of the strings representation have a '0'. If both strings don't have a '0', we found a valid answer and return a list containing [a, n-a]
. The loop should always find a valid answer for the given constraints.
std::vector<int> noZeroIntegers(int n) {
for (int a = 1; a < n; ++a) {
if (std::to_string(a).find('0') == std::string::npos &&
std::to_string(n - a).find('0') == std::string::npos) {
return {a, n - a};
}
}
return {0, 0}; // never gets here
}
We will write a function that receives an integer n
as the input. The algorithm then iterates over all integers from 1
to n-1
as a candidate for value a
in the result [a, b]
. For each candidate a
, we check if both a
and n-a
(which would be b
) are No-Zero integers by ensuring that neither of the strings representation have a '0'. If both strings don't have a '0', we found a valid answer and return a list containing [a, n-a]
. The loop should always find a valid answer for the given constraints.