Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
A no-zero integer is a positive integer that does not contain the digit 0 in its decimal representation.
Given an integer n, count the number of pairs (a, b) where:
a and b are no-zero integers.a + b = nReturn an integer denoting the number of such pairs.
Example 1:
Input: n = 2
Output: 1
Explanation:
The only pair is (1, 1).
Example 2:
Input: n = 3
Output: 2
Explanation:
The pairs are (1, 2) and (2, 1).
Example 3:
Input: n = 11
Output: 8
Explanation:
The pairs are (2, 9), (3, 8), (4, 7), (5, 6), (6, 5), (7, 4), (8, 3), and (9, 2). Note that (1, 10) and (10, 1) do not satisfy the conditions because 10 contains 0 in its decimal representation.
Constraints:
2 <= n <= 1015Problem summary: A no-zero integer is a positive integer that does not contain the digit 0 in its decimal representation. Given an integer n, count the number of pairs (a, b) where: a and b are no-zero integers. a + b = n Return an integer denoting the number of such pairs.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Dynamic Programming
2
3
11
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3704: Count No-Zero Pairs That Sum to N
class Solution {
public long countNoZeroPairs(long n) {
char[] cs = Long.toString(n).toCharArray();
int m = cs.length;
int[] digits = new int[m + 1];
for (int i = 0; i < m; i++) {
digits[i] = cs[m - 1 - i] - '0';
}
digits[m] = 0;
long[][][] dp = new long[2][2][2];
dp[0][1][1] = 1;
for (int pos = 0; pos < m + 1; pos++) {
long[][][] ndp = new long[2][2][2];
int target = digits[pos];
for (int carry = 0; carry <= 1; carry++) {
for (int aliveA = 0; aliveA <= 1; aliveA++) {
for (int aliveB = 0; aliveB <= 1; aliveB++) {
long ways = dp[carry][aliveA][aliveB];
if (ways == 0) {
continue;
}
int[] aDigits;
int[] aNext;
if (aliveA == 1) {
if (pos == 0) {
aDigits = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
aNext = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1};
} else {
aDigits = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
aNext = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 0};
}
} else {
aDigits = new int[] {0};
aNext = new int[] {0};
}
int[] bDigits;
int[] bNext;
if (aliveB == 1) {
if (pos == 0) {
bDigits = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
bNext = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1};
} else {
bDigits = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
bNext = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 0};
}
} else {
bDigits = new int[] {0};
bNext = new int[] {0};
}
for (int ai = 0; ai < aDigits.length; ai++) {
int da = aDigits[ai];
int na = aNext[ai];
for (int bi = 0; bi < bDigits.length; bi++) {
int db = bDigits[bi];
int nb = bNext[bi];
int s = da + db + carry;
if (s % 10 != target) {
continue;
}
int ncarry = s / 10;
ndp[ncarry][na][nb] += ways;
}
}
}
}
}
dp = ndp;
}
return dp[0][0][0];
}
public long countPairs(long n) {
return countNoZeroPairs(n);
}
}
// Accepted solution for LeetCode #3704: Count No-Zero Pairs That Sum to N
package main
import "strconv"
func countNoZeroPairs(n int64) int64 {
s := []byte(strconv.FormatInt(n, 10))
m := len(s)
digits := make([]int, m+1)
for i := 0; i < m; i++ {
digits[i] = int(s[m-1-i] - '0')
}
digits[m] = 0
var dp [2][2][2]int64
dp[0][1][1] = 1
for pos := 0; pos < m+1; pos++ {
var ndp [2][2][2]int64
target := digits[pos]
for carry := 0; carry <= 1; carry++ {
for aliveA := 0; aliveA <= 1; aliveA++ {
for aliveB := 0; aliveB <= 1; aliveB++ {
ways := dp[carry][aliveA][aliveB]
if ways == 0 {
continue
}
var A [10][2]int
aLen := 0
if aliveA == 1 {
for d := 1; d <= 9; d++ {
A[aLen][0] = d
A[aLen][1] = 1
aLen++
}
if pos > 0 {
A[aLen][0] = 0
A[aLen][1] = 0
aLen++
}
} else {
A[0][0] = 0
A[0][1] = 0
aLen = 1
}
var B [10][2]int
bLen := 0
if aliveB == 1 {
for d := 1; d <= 9; d++ {
B[bLen][0] = d
B[bLen][1] = 1
bLen++
}
if pos > 0 {
B[bLen][0] = 0
B[bLen][1] = 0
bLen++
}
} else {
B[0][0] = 0
B[0][1] = 0
bLen = 1
}
for ai := 0; ai < aLen; ai++ {
da, na := A[ai][0], A[ai][1]
for bi := 0; bi < bLen; bi++ {
db, nb := B[bi][0], B[bi][1]
sum := da + db + carry
if sum%10 != target {
continue
}
ncarry := sum / 10
ndp[ncarry][na][nb] += ways
}
}
}
}
}
dp = ndp
}
return dp[0][0][0]
}
func countPairs(n int64) int64 {
return countNoZeroPairs(n)
}
# Accepted solution for LeetCode #3704: Count No-Zero Pairs That Sum to N
class Solution:
def countNoZeroPairs(self, n: int) -> int:
digits = list(map(int, str(n)))[::-1]
digits.append(0) # absorb final carry
L = len(digits)
# dp[carry][aliveA][aliveB]
dp = [[[0] * 2 for _ in range(2)] for _ in range(2)]
dp[0][1][1] = 1
for pos in range(L):
ndp = [[[0] * 2 for _ in range(2)] for _ in range(2)]
target = digits[pos]
for carry in range(2):
for aliveA in range(2):
for aliveB in range(2):
ways = dp[carry][aliveA][aliveB]
if ways == 0:
continue
if aliveA:
A = [(d, 1) for d in range(1, 10)]
if pos > 0:
A.append((0, 0)) # end number here
else:
A = [(0, 0)]
if aliveB:
B = [(d, 1) for d in range(1, 10)]
if pos > 0:
B.append((0, 0))
else:
B = [(0, 0)]
for da, na in A:
for db, nb in B:
s = da + db + carry
if s % 10 != target:
continue
ndp[s // 10][na][nb] += ways
dp = ndp
return dp[0][0][0]
def countPairs(self, n: int) -> int:
return self.countNoZeroPairs(n)
// Accepted solution for LeetCode #3704: Count No-Zero Pairs That Sum to N
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3704: Count No-Zero Pairs That Sum to N
// class Solution {
// public long countNoZeroPairs(long n) {
// char[] cs = Long.toString(n).toCharArray();
// int m = cs.length;
// int[] digits = new int[m + 1];
// for (int i = 0; i < m; i++) {
// digits[i] = cs[m - 1 - i] - '0';
// }
// digits[m] = 0;
//
// long[][][] dp = new long[2][2][2];
// dp[0][1][1] = 1;
//
// for (int pos = 0; pos < m + 1; pos++) {
// long[][][] ndp = new long[2][2][2];
// int target = digits[pos];
// for (int carry = 0; carry <= 1; carry++) {
// for (int aliveA = 0; aliveA <= 1; aliveA++) {
// for (int aliveB = 0; aliveB <= 1; aliveB++) {
// long ways = dp[carry][aliveA][aliveB];
// if (ways == 0) {
// continue;
// }
// int[] aDigits;
// int[] aNext;
// if (aliveA == 1) {
// if (pos == 0) {
// aDigits = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
// aNext = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1};
// } else {
// aDigits = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
// aNext = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 0};
// }
// } else {
// aDigits = new int[] {0};
// aNext = new int[] {0};
// }
//
// int[] bDigits;
// int[] bNext;
// if (aliveB == 1) {
// if (pos == 0) {
// bDigits = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
// bNext = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1};
// } else {
// bDigits = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
// bNext = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 0};
// }
// } else {
// bDigits = new int[] {0};
// bNext = new int[] {0};
// }
//
// for (int ai = 0; ai < aDigits.length; ai++) {
// int da = aDigits[ai];
// int na = aNext[ai];
// for (int bi = 0; bi < bDigits.length; bi++) {
// int db = bDigits[bi];
// int nb = bNext[bi];
// int s = da + db + carry;
// if (s % 10 != target) {
// continue;
// }
// int ncarry = s / 10;
// ndp[ncarry][na][nb] += ways;
// }
// }
// }
// }
// }
// dp = ndp;
// }
//
// return dp[0][0][0];
// }
//
// public long countPairs(long n) {
// return countNoZeroPairs(n);
// }
// }
// Accepted solution for LeetCode #3704: Count No-Zero Pairs That Sum to N
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #3704: Count No-Zero Pairs That Sum to N
// class Solution {
// public long countNoZeroPairs(long n) {
// char[] cs = Long.toString(n).toCharArray();
// int m = cs.length;
// int[] digits = new int[m + 1];
// for (int i = 0; i < m; i++) {
// digits[i] = cs[m - 1 - i] - '0';
// }
// digits[m] = 0;
//
// long[][][] dp = new long[2][2][2];
// dp[0][1][1] = 1;
//
// for (int pos = 0; pos < m + 1; pos++) {
// long[][][] ndp = new long[2][2][2];
// int target = digits[pos];
// for (int carry = 0; carry <= 1; carry++) {
// for (int aliveA = 0; aliveA <= 1; aliveA++) {
// for (int aliveB = 0; aliveB <= 1; aliveB++) {
// long ways = dp[carry][aliveA][aliveB];
// if (ways == 0) {
// continue;
// }
// int[] aDigits;
// int[] aNext;
// if (aliveA == 1) {
// if (pos == 0) {
// aDigits = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
// aNext = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1};
// } else {
// aDigits = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
// aNext = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 0};
// }
// } else {
// aDigits = new int[] {0};
// aNext = new int[] {0};
// }
//
// int[] bDigits;
// int[] bNext;
// if (aliveB == 1) {
// if (pos == 0) {
// bDigits = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
// bNext = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1};
// } else {
// bDigits = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
// bNext = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 0};
// }
// } else {
// bDigits = new int[] {0};
// bNext = new int[] {0};
// }
//
// for (int ai = 0; ai < aDigits.length; ai++) {
// int da = aDigits[ai];
// int na = aNext[ai];
// for (int bi = 0; bi < bDigits.length; bi++) {
// int db = bDigits[bi];
// int nb = bNext[bi];
// int s = da + db + carry;
// if (s % 10 != target) {
// continue;
// }
// int ncarry = s / 10;
// ndp[ncarry][na][nb] += ways;
// }
// }
// }
// }
// }
// dp = ndp;
// }
//
// return dp[0][0][0];
// }
//
// public long countPairs(long n) {
// return countNoZeroPairs(n);
// }
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.