Convert Integer to the Sum of Two No-Zero Integers - Problem

A 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.

Input & Output

Example 1 — Basic Case
$ Input: n = 2
Output: [1,1]
💡 Note: 1 + 1 = 2. Both 1 and 1 are no-zero integers (contain no digit 0)
Example 2 — Larger Number
$ Input: n = 11
Output: [2,9]
💡 Note: 2 + 9 = 11. Both 2 and 9 contain no zeros. Note: [1,10] would be invalid since 10 contains 0
Example 3 — Three Digit Number
$ Input: n = 101
Output: [2,99]
💡 Note: 2 + 99 = 101. Both 2 and 99 contain no zeros. Note: [1,100] would be invalid since 100 contains 0

Constraints

  • 2 ≤ n ≤ 104

Visualization

Tap to expand
No-Zero Integer Sum INPUT Integer n 2 Goal: Find a + b = n where a and b have NO zeros in them (10, 20, 101 are invalid) Valid No-Zero integers: 1 2 11 99 Invalid (contains 0): 10 101 200 ALGORITHM STEPS 1 Start with a = 1 Begin with smallest No-Zero 2 Calculate b = n - a b = 2 - 1 = 1 3 Check both values Are a and b No-Zero? 4 Return or increment If valid: return [a, b] Else: a++ and repeat Checking a=1, b=1: hasZero(1)? NO hasZero(1)? NO OK - Both valid! FINAL RESULT Output Array 1 a 1 b Verification: 1 + 1 = 2 OK Both are No-Zero! (no 0 digits) Return Value: [1, 1] Key Insight: Start with a=1 and increment. For each a, compute b=n-a. Check if both contain no '0' digit by converting to string or using modulo 10. Since n is at least 2, we always find a valid pair quickly. TutorialsPoint - Convert Integer to the Sum of Two No-Zero Integers | Optimized - Start Simple and Adjust
Asked in
Amazon 15 Google 12 Microsoft 8
15.2K Views
Medium Frequency
~15 min Avg. Time
428 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen