Given a number N, the task is to print the first N Stepping Numbers.
A number is called stepping number if all adjacent digits have an absolute difference of 1. For e.g. 321 is a Stepping Number while 421 is not.
Input: N = 7
Output: 1, 2, 3, 4, 5, 6, 7
Input: N = 14
Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 21, 22
Naive approach: We can start from 1 and check for every number whether they are Stepping number or not and continue till we find the K-th Stepping number.
- Generate all possible Stepping numbers till 1000, for easy computation
- For each value of N, just print the N already computed Stepping numbers
Below is the implementation of the above approach:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 21, 22,
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Number of n digit stepping numbers
- Program to print numbers from N to 1 in reverse order
- Recursive program to print all numbers less than N which consist of digits 1 or 3 only
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- How will you print numbers from 1 to 100 without using loop?
- Print numbers with digits 0 and 1 only such that their sum is N
- How will you print numbers from 1 to 100 without using loop? | Set-2
- Print N-bit binary numbers having more 1’s than 0’s in all prefixes
- Print N numbers such that their sum is a Perfect Cube
- Print all numbers less than N with at-most 2 unique digits
- Print all Good numbers in given range
- Print all n-digit numbers whose sum of digits equals to given sum
- Print all n-digit strictly increasing numbers
- Print all increasing sequences of length k from first n natural numbers
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
Improved By : Sanjit_Prasad