Count Good Numbers

A digit string is good if the digits at even indices (0-indexed) are even and the digits at odd indices are prime (2, 3, 5 or 7). Given an integer n, return the number of good digit strings of length n, modulo 10^9 + 7. Leading zeros are allowed.

30:00

Count Good Numbers
medium
Free written solution
Topics
Companies

A digit string is good if the digits at even indices (0-indexed) are even and the digits at odd indices are prime (2, 3, 5 or 7). Given an integer n, return the number of good digit strings of length n, modulo 10^9 + 7. Leading zeros are allowed.

Example 1:
Input: {"n":1}
Output: 5
Constraints:
  • 1≤n≤231−11 \leq n \leq 2^{31} - 1 (LeetCode allows up to 101510^{15}; here n is a 32-bit int)

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":1}

A good number of length 1 has a digit choice at every index: 5 even digits at even indices, 4 primes at odd indices.

digit positions of a length-1 number (choices per slot)

5

02468

0

count = 5^? × 4^? mod 1e9+7

Variables
VariableValue
n1
DepthFunction Call
Stack empty
0/9