Given two integers n and k, return every integer with exactly n digits in which the absolute difference between every two consecutive digits is k.
The integers must not have leading zeros, so values such as 02 or 043 are not allowed. A 0 digit is fine in any position after the first.
Function Signature
def numbers_with_consecutive_difference(n: int, k: int) -> list[int]:
Rules
-
Return the numbers sorted in ascending order, with each number appearing once.
-
Every returned number has exactly
n
digits, and its first digit is between 1 and 9.
Constraints
Examples
Input: n = 3, k = 7
Output: [181, 292, 707, 818, 929]
The digits 3, 4, 5 and 6 have no digit 7 away from them, so only 1, 2, 7, 8 and 9 can start a number. Each of the two-digit prefixes 18, 29, 70, 81 and 92 then has exactly one valid third digit.
Input: n = 2, k = 1
Output: [10, 12, 21, 23, 32, 34, 43, 45, 54, 56, 65, 67, 76, 78, 87, 89, 98]
First digits 1 through 8 each allow a second digit one below and one above, and 9 allows only 8, which gives 17 numbers. 10 is valid because its zero is not leading.
Input: n = 2, k = 0
Output: [11, 22, 33, 44, 55, 66, 77, 88, 99]
With k = 0 every digit must repeat the previous one, so each first digit gives exactly one number.