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.
Move from brute-force thinking to an efficient approach using math strategy.
A digit string is good if the digits (0-indexed) at even indices are even and the digits at odd indices are prime (2, 3, 5, or 7).
"2582" is good because the digits (2 and 8) at even positions are even and the digits (5 and 2) at odd positions are prime. However, "3245" is not good because 3 is at an even index but is not even.Given an integer n, return the total number of good digit strings of length n. Since the answer may be large, return it modulo 109 + 7.
A digit string is a string consisting of digits 0 through 9 that may contain leading zeros.
Example 1:
Input: n = 1 Output: 5 Explanation: The good numbers of length 1 are "0", "2", "4", "6", "8".
Example 2:
Input: n = 4 Output: 400
Example 3:
Input: n = 50 Output: 564908303
Constraints:
1 <= n <= 1015Problem summary: A digit string is good if the digits (0-indexed) at even indices are even and the digits at odd indices are prime (2, 3, 5, or 7). For example, "2582" is good because the digits (2 and 8) at even positions are even and the digits (5 and 2) at odd positions are prime. However, "3245" is not good because 3 is at an even index but is not even. Given an integer n, return the total number of good digit strings of length n. Since the answer may be large, return it modulo 109 + 7. A digit string is a string consisting of digits 0 through 9 that may contain leading zeros.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
1
4
50
count-the-number-of-arrays-with-k-matching-adjacent-elements)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1922: Count Good Numbers
class Solution {
private final int mod = (int) 1e9 + 7;
public int countGoodNumbers(long n) {
return (int) (qpow(5, (n + 1) >> 1) * qpow(4, n >> 1) % mod);
}
private long qpow(long x, long n) {
long res = 1;
while (n != 0) {
if ((n & 1) == 1) {
res = res * x % mod;
}
x = x * x % mod;
n >>= 1;
}
return res;
}
}
// Accepted solution for LeetCode #1922: Count Good Numbers
const mod int64 = 1e9 + 7
func countGoodNumbers(n int64) int {
return int(myPow(5, (n+1)>>1) * myPow(4, n>>1) % mod)
}
func myPow(x, n int64) int64 {
var res int64 = 1
for n != 0 {
if (n & 1) == 1 {
res = res * x % mod
}
x = x * x % mod
n >>= 1
}
return res
}
# Accepted solution for LeetCode #1922: Count Good Numbers
class Solution:
def countGoodNumbers(self, n: int) -> int:
mod = 10**9 + 7
return pow(5, (n + 1) >> 1, mod) * pow(4, n >> 1, mod) % mod
// Accepted solution for LeetCode #1922: Count Good Numbers
/**
* [1922] Count Good Numbers
*
* A digit string is good if the digits (0-indexed) at even indices are even and the digits at odd indices are prime (2, 3, 5, or 7).
*
* For example, "2582" is good because the digits (2 and 8) at even positions are even and the digits (5 and 2) at odd positions are prime. However, "3245" is not good because 3 is at an even index but is not even.
*
* Given an integer n, return the total number of good digit strings of length n. Since the answer may be large, return it modulo 10^9 + 7.
* A digit string is a string consisting of digits 0 through 9 that may contain leading zeros.
*
* Example 1:
*
* Input: n = 1
* Output: 5
* Explanation: The good numbers of length 1 are "0", "2", "4", "6", "8".
*
* Example 2:
*
* Input: n = 4
* Output: 400
*
* Example 3:
*
* Input: n = 50
* Output: 564908303
*
*
* Constraints:
*
* 1 <= n <= 10^15
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/count-good-numbers/
// discuss: https://leetcode.com/problems/count-good-numbers/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn count_good_numbers(n: i64) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_1922_example_1() {
let n = 1;
let result = 5;
assert_eq!(Solution::count_good_numbers(n), result);
}
#[test]
#[ignore]
fn test_1922_example_2() {
let n = 4;
let result = 400;
assert_eq!(Solution::count_good_numbers(n), result);
}
#[test]
#[ignore]
fn test_1922_example_3() {
let n = 50;
let result = 564908303;
assert_eq!(Solution::count_good_numbers(n), result);
}
}
// Accepted solution for LeetCode #1922: Count Good Numbers
function countGoodNumbers(n: number): number {
const mod = 1000000007n;
const qpow = (x: bigint, n: bigint): bigint => {
let res = 1n;
while (n > 0n) {
if (n & 1n) {
res = (res * x) % mod;
}
x = (x * x) % mod;
n >>= 1n;
}
return res;
};
const a = qpow(5n, BigInt(n + 1) / 2n);
const b = qpow(4n, BigInt(n) / 2n);
return Number((a * b) % mod);
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
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.