Ctrl K

Numbers With a Fixed Digit Step

Medium
You get two integers, length and step. Find every number with exactly length digits in which any two neighboring digits differ by exactly step (in absolute value). Leading zeros are not allowed: something like 07 or 0707 does not count as a number of that length. Return the numbers in any order.
Example 1
length=3, step=8 → [191, 808, 919]

Neighboring digits must differ by 8, which only allows the pairs 0–8 and 1–9. A number like 080 starts with a zero, so 808, 191 and 919 remain.

Example 2
length=2, step=0 → [11, 22, 33, 44, 55, 66, 77, 88, 99]

Both digits are equal: 11, 22, ..., 99. 00 does not count, as it is not a two-digit number.

Constraints
  • 2 ≤ length ≤ 9
  • 0 ≤ step ≤ 9
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsEvery Route to the Exit MediumEvery Spelling of a Pattern MediumNumbers With Neighbouring Digits MediumSix-Cell Tile Slider HardExpanding Curly Braces Hard
loading Python…
Input
length=3, step=8
Expected
[191, 808, 919]

Run checks the first 3 tests in your browser. Submit checks all 63 tests on the server, hidden ones included.