FastPrepStepping Numbers Up to N

Stepping Numbers Up to N

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

A positive integer is a stepping number when the absolute difference between every pair of adjacent decimal digits is exactly 1. Every one-digit positive integer is a stepping number.

Given n, return all stepping numbers in the inclusive range [1, n] in increasing order.

Function

steppingNumbers(n: int) → int[]

Examples

Example 1

n = 20return = [1,2,3,4,5,6,7,8,9,10,12]

All one-digit values qualify. Among two-digit values at most twenty, only 10 and 12 have adjacent digits differing by one.

Example 2

n = 30return = [1,2,3,4,5,6,7,8,9,10,12,21,23]

Compared with the first example, 21 and 23 also qualify.

Example 3

n = 9return = [1,2,3,4,5,6,7,8,9]

Every positive one-digit number is a stepping number.

Constraints

  • 1 <= n <= 1000000000.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int[] steppingNumbers(int n) {
    // Write your code here.
}
n20
expected[1,2,3,4,5,6,7,8,9,10,12]
Checking account…