> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Numbers With Same Consecutive Differences

> Tested Python solution for LeetCode 967 with 24 pytest cases. Generate a practice environment with lcpy.

LeetCode 967, [Medium](/catalog/medium). Topics: [Backtracking](/catalog/topics/backtracking), [Breadth-First Search](/catalog/topics/breadth-first-search). [View on LeetCode](https://leetcode.com/problems/numbers-with-same-consecutive-differences/description/).

Generate this problem as a practice environment: tested reference solution, 24 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 967   # by problem number
lcpy gen -s numbers_with_same_consecutive_differences   # by problem name
```

## Problem

Given two integers `n` and `k`, return an array of all the integers of length `n` where the difference between every two consecutive digits is `k`. You may return the answer in any order.

Note that the integers should not have leading zeros. Integers as `02` and `043` are not allowed.

### Examples

```
Input: n = 3, k = 7
Output: [181,292,707,818,929]
Explanation: Note that 070 is not a valid number, because it has leading zeroes.
```

```
Input: n = 2, k = 1
Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98]
```

### Constraints

* `2 <= n <= 9`
* `0 <= k <= 9`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/numbers_with_same_consecutive_differences/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/numbers_with_same_consecutive_differences/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(2^n * n) over digit sequences of length n
    # Space: O(2^n) for the answer
    def nums_same_consec_diff(self, n: int, k: int) -> list[int]:
        digits = list(range(1, 10))
        for _ in range(n - 1):
            nxt: list[int] = []
            for num in digits:
                last = num % 10
                if last + k <= 9:
                    nxt.append(num * 10 + last + k)
                if k and last - k >= 0:
                    nxt.append(num * 10 + last - k)
            digits = nxt
        return digits
```

## Complexity

| Time | Space |
| - | - |
| O(2^n \* n) over digit sequences of length n | O(2^n) for the answer |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.