Program to count number of stepping numbers of n digits in python

A stepping number is a number where all adjacent digits have an absolute difference of exactly 1. For example, 123 is a stepping number (|1-2|=1, |2-3|=1), but 124 is not (|2-4|=2). We need to count all stepping numbers with exactly n digits.

Understanding the Problem

For n = 2, the stepping numbers are: [10, 12, 21, 23, 32, 34, 43, 45, 54, 56, 65, 67, 76, 78, 87, 89, 98], giving us 17 total numbers.

Dynamic Programming Approach

We use dynamic programming where dp[i] represents the count of stepping numbers ending with digit i. For each position, we build the next digit based on the constraint that adjacent digits differ by 1 ?

def count_stepping_numbers(n):
    MOD = 10**9 + 7
    
    # Base cases
    if n == 0:
        return 0
    if n == 1:
        return 10  # All single digits 0-9
    
    # dp[i] = count of stepping numbers ending with digit i
    dp = [1] * 10
    
    # Build numbers digit by digit
    for _ in range(n - 1):
        new_dp = [0] * 10
        
        # For digit 0, can only come from digit 1
        new_dp[0] = dp[1]
        
        # For digits 1-8, can come from adjacent digits
        for i in range(1, 9):
            new_dp[i] = (dp[i - 1] + dp[i + 1]) % MOD
        
        # For digit 9, can only come from digit 8
        new_dp[9] = dp[8]
        
        dp = new_dp
    
    # Return sum excluding numbers starting with 0
    return sum(dp[1:]) % MOD

# Test the function
print(count_stepping_numbers(2))
print(count_stepping_numbers(3))
17
32

How It Works

The algorithm works by tracking how many stepping numbers end with each digit (0-9). For each new position:

  • Numbers ending in 0 can only be formed by appending 0 to numbers ending in 1
  • Numbers ending in 1-8 can be formed from numbers ending in adjacent digits (i±1)
  • Numbers ending in 9 can only be formed by appending 9 to numbers ending in 8

Alternative Implementation with Class

class SteppingNumbers:
    def solve(self, n):
        MOD = 10**9 + 7
        
        if n == 0:
            return 0
        if n == 1:
            return 10
        
        dp = [1] * 10
        
        for _ in range(n - 1):
            new_dp = [0] * 10
            new_dp[0] = dp[1]
            
            for i in range(1, 9):
                new_dp[i] = (dp[i - 1] + dp[i + 1]) % MOD
            
            new_dp[9] = dp[8]
            dp = new_dp
        
        return sum(dp[1:]) % MOD

# Usage
solution = SteppingNumbers()
print(f"Stepping numbers with 2 digits: {solution.solve(2)}")
print(f"Stepping numbers with 3 digits: {solution.solve(3)}")
print(f"Stepping numbers with 4 digits: {solution.solve(4)}")
Stepping numbers with 2 digits: 17
Stepping numbers with 3 digits: 32
Stepping numbers with 4 digits: 56

Time and Space Complexity

  • Time Complexity: O(n), where n is the number of digits
  • Space Complexity: O(1), using only fixed-size arrays of length 10

Conclusion

The dynamic programming solution efficiently counts stepping numbers by tracking how many numbers end with each digit. We exclude numbers starting with 0 since they don't form valid n-digit numbers.

---
Updated on: 2026-03-25T12:57:30+05:30

594 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements