2025-09-08 Daily Challenge
Today I have done leetcode's September LeetCoding Challenge with cpp
.
September LeetCoding Challenge 8
Description
Convert Integer to the Sum of Two No-Zero Integers
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
andb
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
Solution
class Solution {
bool isNonZero(int a) {
while(a) {
if(a % 10 == 0) return false;
a /= 10;
}
return true;
}
public:
vector<int> getNoZeroIntegers(int n) {
vector<int> answer(2);
for(int i = 1; i * 2 <= n; ++i) {
if(isNonZero(i) && isNonZero(n - i)) {
answer[0] = i;
answer[1] = n - i;
}
}
return answer;
}
};
// Accepted
// 207/207 cases passed (1 ms)
// Your runtime beats 10.1 % of cpp submissions
// Your memory usage beats 85.27 % of cpp submissions (8.2 MB)