Given n, find count of n digit Stepping numbers. A number is called stepping number if all adjacent digits have an absolute difference of 1. 321 is a Stepping Number while 421 is not.
Examples :
Input : 2
Output : 17
Explanation: The numbers are 10, 12, 21,
23, 32, 34, 43, 45, 54, 56, 65, 67, 76,
78, 87, 89, 98.
Input : 1
Output : 10
Explanation: the numbers are 0, 1, 2, 3,
4, 5, 6, 7, 8, 9.
A naive approach is to run a loop for all n digit numbers and check for every number if it is Stepping.
An efficient approach is to use dynamic programming.
In dp[i][j], i denotes number of
digits and j denotes last digit.
// If there is only one digit
if (i == 1)
dp(i, j) = 1;
// If last digit is 0.
if (j == 0)
dp(i, j) = dp(i-1, j+1)
// If last digit is 9
else if (j == 9)
dp(i, j) = dp(i-1, j-1)
// If last digit is neither 0
// nor 9.
else
dp(i, j) = dp(i-1, j-1) +
dp(i-1, j+1)
Result is ?dp(n, j) where j varies
from 1 to 9.
C++
#include <bits/stdc++.h>
using namespace std;
long long answer( int n)
{
int dp[n + 1][10];
if (n == 1)
return 10;
for ( int j = 0; j <= 9; j++)
dp[1][j] = 1;
for ( int i = 2; i <= n; i++) {
for ( int j = 0; j <= 9; j++) {
if (j == 0)
dp[i][j] = dp[i - 1][j + 1];
else if (j == 9)
dp[i][j] = dp[i - 1][j - 1];
else
dp[i][j] = dp[i - 1][j - 1] +
dp[i - 1][j + 1];
}
}
long long sum = 0;
for ( int j = 1; j <= 9; j++)
sum += dp[n][j];
return sum;
}
int main()
{
int n = 2;
cout << answer(n);
return 0;
}
|
Java
class GFG {
static long answer( int n)
{
int dp[][] = new int [n+ 1 ][ 10 ];
if (n == 1 )
return 10 ;
for ( int j = 0 ; j <= 9 ; j++)
dp[ 1 ][j] = 1 ;
for ( int i = 2 ; i <= n; i++) {
for ( int j = 0 ; j <= 9 ; j++) {
if (j == 0 )
dp[i][j] = dp[i - 1 ][j + 1 ];
else if (j == 9 )
dp[i][j] = dp[i - 1 ][j - 1 ];
else
dp[i][j] = dp[i - 1 ][j - 1 ] +
dp[i - 1 ][j + 1 ];
}
}
long sum = 0 ;
for ( int j = 1 ; j <= 9 ; j++)
sum += dp[n][j];
return sum;
}
public static void main(String args[])
{
int n = 2 ;
System.out.println(answer(n));
}
}
|
Python3
def answer(n):
dp = [[ 0 for x in range ( 10 )]
for y in range (n + 1 )];
if (n = = 1 ):
return 10 ;
for j in range ( 10 ):
dp[ 1 ][j] = 1 ;
for i in range ( 2 , n + 1 ):
for j in range ( 10 ):
if (j = = 0 ):
dp[i][j] = dp[i - 1 ][j + 1 ];
elif (j = = 9 ):
dp[i][j] = dp[i - 1 ][j - 1 ];
else :
dp[i][j] = (dp[i - 1 ][j - 1 ] +
dp[i - 1 ][j + 1 ]);
sum = 0 ;
for j in range ( 1 , 10 ):
sum = sum + dp[n][j];
return sum ;
n = 2 ;
print (answer(n));
|
C#
using System;
class GFG {
static long answer( int n)
{
int [,]dp = new int [n+1,10];
if (n == 1)
return 10;
for ( int j = 0; j <= 9; j++)
dp[1,j] = 1;
for ( int i = 2; i <= n; i++) {
for ( int j = 0; j <= 9; j++) {
if (j == 0)
dp[i,j] = dp[i - 1,j + 1];
else if (j == 9)
dp[i,j] = dp[i - 1,j - 1];
else
dp[i,j] = dp[i - 1,j - 1] +
dp[i - 1,j + 1];
}
}
long sum = 0;
for ( int j = 1; j <= 9; j++)
sum += dp[n,j];
return sum;
}
public static void Main()
{
int n = 2;
Console.WriteLine(answer(n));
}
}
|
PHP
<?php
function answer( $n )
{
if ( $n == 1)
return 10;
for ( $j = 0; $j <= 9; $j ++)
$dp [1][ $j ] = 1;
for ( $i = 2; $i <= $n ; $i ++)
{
for ( $j = 0; $j <= 9; $j ++)
{
if ( $j == 0)
$dp [ $i ][ $j ] = $dp [ $i - 1][ $j + 1];
else if ( $j == 9)
$dp [ $i ][ $j ] = $dp [ $i - 1][ $j - 1];
else
$dp [ $i ][ $j ] = $dp [ $i - 1][ $j - 1] +
$dp [ $i - 1][ $j + 1];
}
}
$sum = 0;
for ( $j = 1; $j <= 9; $j ++)
$sum += $dp [ $n ][ $j ];
return $sum ;
}
$n = 2;
echo answer( $n );
?>
|
Javascript
<script>
function answer(n)
{
let dp = new Array(n + 1);
for ( var i = 0; i < dp.length; i++)
{
dp[i] = new Array(2);
}
if (n == 1)
return 10;
for (let j = 0; j <= 9; j++)
dp[1][j] = 1;
for (let i = 2; i <= n; i++)
{
for (let j = 0; j <= 9; j++)
{
if (j == 0)
dp[i][j] = dp[i - 1][j + 1];
else if (j == 9)
dp[i][j] = dp[i - 1][j - 1];
else
dp[i][j] = dp[i - 1][j - 1] +
dp[i - 1][j + 1];
}
}
let sum = 0;
for (let j = 1; j <= 9; j++)
sum += dp[n][j];
return sum;
}
let n = 2;
document.write(answer(n));
</script>
|
Time Complexity: O(n), as we are using a loop to traverse n times and within each iteration there are 10 cases. So a total of 10*N that is equivalent to n.
Auxiliary Space: O(n), as we are using extra space of 10*n for dp array.
Efficient approach: Space optimization
In the previous approach, the current value dp[i][j] is only depend upon the current and previous row values of DP. So to optimize the space complexity we use a single 1D array to store the computations.
Implementation steps:
- Create a 1D vector dp of size 10 and initialize it with 1.
- Set a base case.
- Now iterate over subproblems by the help of nested loop and get the current value from previous computations.
- Create a variable sum and and update it by iterating over DP.
- At last return and print the final answer stored in sum .
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
long long answer( int n)
{
vector< long long > dp(10, 1);
if (n == 1)
return 10;
for ( int i = 2; i <= n; i++) {
vector< long long > new_dp(10, 0);
new_dp[0] = dp[1];
new_dp[9] = dp[8];
for ( int j = 1; j <= 8; j++)
new_dp[j] = dp[j - 1] + dp[j + 1];
dp = new_dp;
}
long long sum = 0;
for ( int j = 1; j <= 9; j++)
sum += dp[j];
return sum;
}
int main()
{
int n = 2;
cout << answer(n);
return 0;
}
|
Java
import java.util.*;
public class Main
{
static long answer( int n)
{
ArrayList<Long> dp = new ArrayList<>(Collections.nCopies( 10 , 1L));
if (n == 1 )
return 10 ;
for ( int i = 2 ; i <= n; i++) {
ArrayList<Long> new_dp = new ArrayList<>(Collections.nCopies( 10 , 0L));
new_dp.set( 0 , dp.get( 1 ));
new_dp.set( 9 , dp.get( 8 ));
for ( int j = 1 ; j <= 8 ; j++)
new_dp.set(j, dp.get(j - 1 ) + dp.get(j + 1 ));
dp = new_dp;
}
long sum = 0 ;
for ( int j = 1 ; j <= 9 ; j++)
sum += dp.get(j);
return sum;
}
public static void main(String[] args) {
int n = 2 ;
System.out.println(answer(n));
}
}
|
Python3
def answer(n):
dp = [ 1 ] * 10
if n = = 1 :
return 10
for i in range ( 2 , n + 1 ):
new_dp = [ 0 ] * 10
new_dp[ 0 ] = dp[ 1 ]
new_dp[ 9 ] = dp[ 8 ]
for j in range ( 1 , 9 ):
new_dp[j] = dp[j - 1 ] + dp[j + 1 ]
dp = new_dp
sum = 0
for j in range ( 1 , 10 ):
sum + = dp[j]
return sum
n = 2
print (answer(n))
|
C#
using System;
using System.Collections.Generic;
class SteppingNumbers {
static long Answer( int n)
{
List< long > dp = new List< long >();
for ( int i = 0; i < 10; i++) {
dp.Add(1);
}
if (n == 1) {
return 10;
}
for ( int i = 2; i <= n; i++) {
List< long > new_dp = new List< long >();
for ( int j = 0; j < 10; j++) {
new_dp.Add(0);
}
new_dp[0] = dp[1];
new_dp[9] = dp[8];
for ( int j = 1; j <= 8; j++) {
new_dp[j] = dp[j - 1] + dp[j + 1];
}
dp = new_dp;
}
long sum = 0;
for ( int j = 1; j <= 9; j++) {
sum += dp[j];
}
return sum;
}
static void Main()
{
int n = 2;
Console.WriteLine(Answer(n));
}
}
|
Javascript
function answer(n) {
let dp = new Array(10).fill(1);
if (n == 1)
return 10;
for (let i = 2; i <= n; i++) {
let new_dp = new Array(10).fill(0);
new_dp[0] = dp[1];
new_dp[9] = dp[8];
for (let j = 1; j <= 8; j++)
new_dp[j] = dp[j - 1] + dp[j + 1];
dp = new_dp;
}
let sum = 0;
for (let j = 1; j <= 9; j++)
sum += dp[j];
return sum;
}
let n = 2;
console.log(answer(n));
|
Time complexity: O(n)
Auxiliary Space: O(10) => O(1)
Number of n digit stepping numbers | Space optimized solution
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
24 Apr, 2023
Like Article
Save Article