1317. Convert Integer to the Sum of Two No-Zero Integers
Description
Given an integer n
. No-Zero integer is a positive integer which doesn't contain any 0 in its decimal representation.
Return a list of two integers [A, B]
where:
A
andB
are No-Zero integers.A + B = n
It's guarateed 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: A = 1, B = 1. A + B = n and both A and B don't contain any 0 in their decimal representation.
Example 2:
Input: n = 11 Output: [2,9]
Example 3:
Input: n = 10000 Output: [1,9999]
Example 4:
Input: n = 69 Output: [1,68]
Example 5:
Input: n = 1010 Output: [11,999]
Constraints:
2 <= n <= 10^4
My Solution
Source Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* @param {number} n
* @return {number[]}
*/
let getNoZeroIntegers = function(n) {
let n1 = n - 1;
let n2 = n - n1;
while (n1.toString().indexOf("0") != -1 || n2.toString().indexOf("0") != -1) {
n1--;
n2 = n - n1;
}
return [n1, n - n1];
};
Analysis
My solution is to just brute force it. We iterate over integers n down to 1 and stop as soon as we find two valid summands.
I think there's a better way to do this with some number theory but I think that's really overcomplicating things. I may revisit this problem if I can think of a really neat solution.