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 = 11 = n. Note that there are other valid answers as [8, 3] that can be accepted.
Constraints:
2 <= n <= 104
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to find two numbers that add up to a given number, but neither of these numbers can contain the digit zero. The brute force way to do this is to simply try every possible pair of numbers.
Here's how the algorithm would work step-by-step:
def convert_integer_to_sum_of_two_no_zero_integers(number_to_convert):
first_number = 1
while first_number < number_to_convert:
second_number = number_to_convert - first_number
# Checking if the current numbers contain zero
if '0' not in str(first_number) and '0' not in str(second_number):
return [first_number, second_number]
first_number += 1
return []
The goal is to split a number into two parts, ensuring neither part contains the digit zero. The best way is to start with one part as 1 and increment it, checking if both parts satisfy the 'no-zero' condition.
Here's how the algorithm would work step-by-step:
def convert_integer_to_sum_of_two_no_zero_integers(number):
first_number = 1
while True:
second_number = number - first_number
# Check if either number contains a zero.
if '0' in str(first_number) or '0' in str(second_number):
first_number += 1
else:
# Found valid numbers, return as a list.
return [first_number, second_number]
Case | How to Handle |
---|---|
Input n is less than 2 | Return an appropriate error or an empty array since no solution exists as A and B must be positive integers |
Input n is a multiple of 10 | The standard solution might initially create A or B with a 0, so adjust A and B until both are no-zero integers. |
Input n is very large (approaching integer limit) | Check for potential integer overflow when calculating A or B and handle the overflow appropriately |
Input n = 10 | A naive approach might result in A=0 and B=10, thus this requires special handling to generate no-zero integers |
A solution exists but the initial choices of A and B both contain zeros. | Iteratively adjust A and B to find a solution where neither contains a zero, potentially decrementing A and incrementing B. |
No valid A and B exist that satisfy the no-zero constraint. | After a reasonable number of attempts, return an empty array or error to indicate no solution could be found. |
The optimal A or B is close to either 1 or n-1. | Ensure the algorithm correctly handles boundary conditions when one of the numbers is very small or very large. |
Input n is negative or zero | Return an error or throw an exception, as the problem states A and B must be positive integers. |