Tensorum
LeetCode 1922 · Recursion Basics

Count Good Numbers

LeetCode 1922 · Medium · solution, intuition & step-by-step walkthrough

5^ceil(n/2) · 4^floor(n/2) mod 1e9+7.

MediumMathRecursion
0 msruntime · beats 100%
19.3 MBmemory · beats 12%
2approaches, in order
1accepted runs · last 2 Apr 2026

This is how I solved Count Good Numbers (LeetCode 1922), a medium problem from the Recursion Basics pattern. Below: the intuition, the approach step by step, my accepted Python code and the 2 versions I wrote on the way, animated test cases and a line-by-line debugger.

Intuition

Even positions can hold 5 digits (0, 2, 4, 6, 8) and odd positions 4 digits (2, 3, 5, 7), independently. So the count is 5^(even positions) × 4^(odd positions), computed with fast modular exponentiation.

Approach

  1. 1

    Backtracking version: build every string — only works for tiny n.

  2. 2

    Even indices: (n + 1) // 2; odd indices: n // 2.

  3. 3

    Fast power mod 1e9+7 for both, then multiply.

How I solved it · from first idea to final

1
BacktrackingO(5^n) time

Enumerate every good string.

2
Fast modular powerAcceptedO(log n) time

Counting formula.

BacktrackingPythonO(5^n) time
class Solution:
    def countGoodNumbers(self, n: int) -> int:
        #0,2,4,6,8 - length 1
        #0/2/4/6/8_ 2/3/5/7_ 02,03,05,07 22 23 25 27
        #if index is even pick even no
        #if index is odd pick odd no
        count = 0
        def dfs(index,ds):
            nonlocal count
            if len(ds)==n:
                count+=1
                return
            if index%2==0:
                nums = ["0","2","4","6","8"]
                for num in nums:
                    ds.append(num)
                    dfs(index+1,ds)
                    ds.pop()
            elif index%2==1:
                nums = ["2","3","5","7"]
                for num in nums:
                    ds.append(num)
                    dfs(index+1,ds)
                    ds.pop()
        dfs(0,[])
        return count
Fast modular powerPythonO(log n) time
class Solution:
    def countGoodNumbers(self, n: int) -> int:
        #0,2,4,6,8 - 5 at even indices
        #2,3,5,7  - 4 at odd indices
        #01234 - a number of length 5 has 5+1/2 even indices - 3 indices[0,2,4], 5//2 - 2 odd indices - 1,3
        #total of good numbers of string of length n is - 5^(n+1)//2*4^(n//2)
        mod = 10**9+7
        def calculate_pow(x,y):
            x=x%mod
            ans = 1
            while y>0:
                if y%2==1:
                    ans = (ans*x)%mod
                    y = y-1
                else:
                    x = (x*x)%mod
                    y = y//2
            return ans
        return (calculate_pow(5,(n+1)//2)*calculate_pow(4,n//2))%mod
Loading the step-through…

Count Good Numbers — FAQ

What is the intuition behind Count Good Numbers?

Even positions can hold 5 digits (0, 2, 4, 6, 8) and odd positions 4 digits (2, 3, 5, 7), independently. So the count is 5^(even positions) × 4^(odd positions), computed with fast modular exponentiation.

How do you solve Count Good Numbers (LeetCode 1922)?

1. Backtracking version: build every string — only works for tiny n. 2. Even indices: (n + 1) // 2; odd indices: n // 2. 3. Fast power mod 1e9+7 for both, then multiply.

What is the time and space complexity of Count Good Numbers?

Time: O(log n). Space: O(1). (Accepted approach: Fast modular power.)

What are common mistakes in Count Good Numbers?

n can be 10¹⁵ — exponentiation by squaring is required. The brute-force version doesn't take the modulo, so it disagrees for large n.

Is there more than one way to solve Count Good Numbers?

Backtracking (O(5^n) time): Enumerate every good string. Fast modular power (O(log n) time): Counting formula.