Convert Integer to the Sum of Two No-Zero Integers
EasyUpdated: Aug 2, 2025
Practice on:
Problem
No-Zero integer is a positive integer that does not contain any0 in its decimal representation.
Given an integer n, return a list of two integers [a, b] where :
aandbare 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.
Examples
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 <= 10^4
Solution
Method 1 – Brute Force Search
Intuition
A no-zero integer is one that does not contain the digit 0. We can try all possible pairs (a, b) such that a + b = n and check if both a and b are no-zero integers. Since n is small, this brute-force approach is efficient enough.
Approach
- Iterate a from 1 to n-1.
- For each a, let b = n - a.
- Check if both a and b are no-zero integers (i.e., their decimal representation does not contain '0').
- Return the first valid pair
[a, b].
Code
C++
class Solution {
public:
vector<int> getNoZeroIntegers(int n) {
for (int a = 1; a < n; ++a) {
int b = n - a;
if (to_string(a).find('0') == string::npos && to_string(b).find('0') == string::npos)
return {a, b};
}
return {};
}
};
Go
func getNoZeroIntegers(n int) []int {
for a := 1; a < n; a++ {
b := n - a
if !strings.Contains(strconv.Itoa(a), "0") && !strings.Contains(strconv.Itoa(b), "0") {
return []int{a, b}
}
}
return nil
}
Java
class Solution {
public int[] getNoZeroIntegers(int n) {
for (int a = 1; a < n; ++a) {
int b = n - a;
if (!String.valueOf(a).contains("0") && !String.valueOf(b).contains("0"))
return new int[]{a, b};
}
return new int[0];
}
}
Kotlin
class Solution {
fun getNoZeroIntegers(n: Int): IntArray {
for (a in 1 until n) {
val b = n - a
if (!a.toString().contains('0') && !b.toString().contains('0'))
return intArrayOf(a, b)
}
return intArrayOf()
}
}
Python
class Solution:
def getNoZeroIntegers(self, n: int) -> list[int]:
for a in range(1, n):
b = n - a
if '0' not in str(a) and '0' not in str(b):
return [a, b]
return []
Rust
impl Solution {
pub fn get_no_zero_integers(n: i32) -> Vec<i32> {
for a in 1..n {
let b = n - a;
if !a.to_string().contains('0') && !b.to_string().contains('0') {
return vec![a, b];
}
}
vec![]
}
}
TypeScript
class Solution {
getNoZeroIntegers(n: number): number[] {
for (let a = 1; a < n; ++a) {
const b = n - a;
if (!a.toString().includes('0') && !b.toString().includes('0'))
return [a, b];
}
return [];
}
}
Complexity
- ⏰ Time complexity:
O(n * log n), as for each a, we check up to n pairs and string conversion/check takes O(log n) time. - 🧺 Space complexity:
O(log n), for string conversion of numbers.