Generate all n-digit numbers whose consecutive digits differ by k

Quick Overview

Generate every n-digit integer whose neighboring digits differ by exactly k, with no leading zeros, and return them in ascending order. It tests level-by-level search or backtracking over digits, careful bounds checks, and handling k equal to zero without producing duplicates.

Generate all n-digit numbers whose consecutive digits differ by k

Company: Flipkart

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

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 ```python 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 - `2 <= n <= 9` - `0 <= k <= 9` ### Examples ```text 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. ```text 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. ```text 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.

Overview: Generate every n-digit integer whose neighboring digits differ by exactly k, with no leading zeros, and return them in ascending order. It tests level-by-level search or backtracking over digits, careful bounds checks, and handling k equal to zero without producing duplicates.

Flipkart logo
Flipkart
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

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

  • 2 <= n <= 9
  • 0 <= k <= 9

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.