# Number of n digit stepping numbers | Space optimized solution

• Last Updated : 22 Jun, 2022

Given n, find the count of n digit Stepping numbers. A number is called a 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
The numbers are 10, 12, 21,
23, 32, 34, 43, 45, 54, 56, 65, 67, 76,
78, 87, 89, 98.

Input : 1
Output : 10
The numbers are 0, 1, 2, 3,
4, 5, 6, 7, 8, 9.```

In the previous post, a solution that requires O(n) auxiliary space is discussed. The auxiliary space required to solve the problem can be optimized. The 2-D dp array dp[i][j] represents count of stepping number of length i and last digit j. For a digit j the count is obtained from digit j – 1 and j + 1. The recurrence relation is dp[i][j] = dp[i-1][j-1] + dp[i-1][j+1] . Observe that the answer for current length i depends only on i – 1. So a 1-D dp array can be used in which for a given i, dp[j] stores count of stepping numbers of length i ending with digit j. Before updating dp array for a given length i, store the result for length i – 1 in another array prev, then update dp array using prev array.
Below is the implementation of above approach:

## C++

 `// CPP program to calculate the number of``// n digit stepping numbers.``#include ``using` `namespace` `std;` `// function that calculates the answer``long` `long` `answer(``int` `n)``{``    ``// dp[j] stores count of i digit``    ``// stepping numbers ending with digit``    ``// j.``    ``int` `dp[10];` `    ``// To store result of length i - 1``    ``// before updating dp[j] for length i.``    ``int` `prev[10];` `    ``// if n is 1 then answer will be 10.``    ``if` `(n == 1)``        ``return` `10;` `    ``// Initialize values for count of``    ``// digits equal to 1.``    ``for` `(``int` `j = 0; j <= 9; j++)``        ``dp[j] = 1;` `    ``// Compute values for count of digits``    ``// more than 1.``    ``for` `(``int` `i = 2; i <= n; i++) {``        ``for` `(``int` `j = 0; j <= 9; j++) {``            ``prev[j] = dp[j];``        ``}` `        ``for` `(``int` `j = 0; j <= 9; j++) {` `            ``// If ending digit is 0``            ``if` `(j == 0)``                ``dp[j] = prev[j + 1];` `            ``// If ending digit is 9``            ``else` `if` `(j == 9)``                ``dp[j] = prev[j - 1];` `            ``// For other digits.``            ``else``                ``dp[j] = prev[j - 1] + prev[j + 1];``        ``}``    ``}` `    ``// stores the final answer``    ``long` `long` `sum = 0;``    ``for` `(``int` `j = 1; j <= 9; j++)``        ``sum += dp[j];``    ``return` `sum;``}` `// driver program to test the above function``int` `main()``{``    ``int` `n = 2;``    ``cout << answer(n);``    ``return` `0;``}`

## Java

 `// Java program to calculate the number of``// n digit stepping numbers.``class` `GFG``{``    ` `// function that calculates the answer``static` `long` `answer(``int` `n)``{``    ``// dp[j] stores count of i digit``    ``// stepping numbers ending with digit``    ``// j.``    ``int``[] dp = ``new` `int``[``10``];` `    ``// To store result of length i - 1``    ``// before updating dp[j] for length i.``    ``int``[] prev = ``new` `int``[``10``];` `    ``// if n is 1 then answer will be 10.``    ``if` `(n == ``1``)``        ``return` `10``;` `    ``// Initialize values for count of``    ``// digits equal to 1.``    ``for` `(``int` `j = ``0``; j <= ``9``; j++)``        ``dp[j] = ``1``;` `    ``// Compute values for count of digits``    ``// more than 1.``    ``for` `(``int` `i = ``2``; i <= n; i++)``    ``{``        ``for` `(``int` `j = ``0``; j <= ``9``; j++)``        ``{``            ``prev[j] = dp[j];``        ``}` `        ``for` `(``int` `j = ``0``; j <= ``9``; j++)``        ``{` `            ``// If ending digit is 0``            ``if` `(j == ``0``)``                ``dp[j] = prev[j + ``1``];` `            ``// If ending digit is 9``            ``else` `if` `(j == ``9``)``                ``dp[j] = prev[j - ``1``];` `            ``// For other digits.``            ``else``                ``dp[j] = prev[j - ``1``] + prev[j + ``1``];``        ``}``    ``}` `    ``// stores the final answer``    ``long` `sum = ``0``;``    ``for` `(``int` `j = ``1``; j <= ``9``; j++)``        ``sum += dp[j];``    ``return` `sum;``}` `// Driver code``public` `static` `void` `main (String[] args)``{``    ``int` `n = ``2``;``    ``System.out.println(answer(n));``}``}` `// This code is contributed by mits`

## Python3

 `# Python3 program to calculate the number of``# n digit stepping numbers.` `# function that calculates the answer``def` `answer(n) :` `    ``# dp[j] stores count of i digit``    ``# stepping numbers ending with digit j.``    ``dp ``=` `[``0``] ``*` `10` `    ``# To store resu1lt of length i - 1``    ``# before updating dp[j] for length i.``    ``prev ``=` `[``0``] ``*` `10` `    ``# if n is 1 then answer will be 10.``    ``if` `(n ``=``=` `1``):``        ``return` `10` `    ``# Initialize values for count of``    ``# digits equal to 1.``    ``for` `j ``in` `range``(``0``, ``10``) :``        ``dp[j] ``=` `1` `    ``# Compute values for count of digits``    ``# more than 1.``    ``for` `i ``in` `range``(``2``, n ``+` `1``):``        ``for` `j ``in` `range` `(``0``, ``10``):``            ``prev[j] ``=` `dp[j]` `        ``for` `j ``in` `range` `(``0``, ``10``):``        ` `            ``# If ending digit is 0``            ``if` `(j ``=``=` `0``):``                ``dp[j] ``=` `prev[j ``+` `1``]` `            ``# If ending digit is 9``            ``elif` `(j ``=``=` `9``) :``                ``dp[j] ``=` `prev[j ``-` `1``]` `            ``# For other digits.``            ``else` `:``                ``dp[j] ``=` `prev[j ``-` `1``] ``+` `prev[j ``+` `1``]``        ` `    ``# stores the final answer``    ``sum` `=` `0``    ``for` `j ``in` `range` `(``1``, ``10``):``        ``sum` `=` `sum` `+` `dp[j]``    ``return` `sum` `# Driver Code``n ``=` `2``print``(answer(n))` `# This code is contributed by ihritik`

## C#

 `// C# program to calculate the number of``// n digit stepping numbers.``using` `System;` `class` `GFG``{``    ` `// function that calculates the answer``static` `long` `answer(``int` `n)``{``    ``// dp[j] stores count of i digit``    ``// stepping numbers ending with digit``    ``// j.``    ``int``[] dp = ``new` `int``[10];` `    ``// To store result of length i - 1``    ``// before updating dp[j] for length i.``    ``int``[] prev = ``new` `int``[10];` `    ``// if n is 1 then answer will be 10.``    ``if` `(n == 1)``        ``return` `10;` `    ``// Initialize values for count of``    ``// digits equal to 1.``    ``for` `(``int` `j = 0; j <= 9; j++)``        ``dp[j] = 1;` `    ``// Compute values for count of digits``    ``// more than 1.``    ``for` `(``int` `i = 2; i <= n; i++)``    ``{``        ``for` `(``int` `j = 0; j <= 9; j++)``        ``{``            ``prev[j] = dp[j];``        ``}` `        ``for` `(``int` `j = 0; j <= 9; j++)``        ``{` `            ``// If ending digit is 0``            ``if` `(j == 0)``                ``dp[j] = prev[j + 1];` `            ``// If ending digit is 9``            ``else` `if` `(j == 9)``                ``dp[j] = prev[j - 1];` `            ``// For other digits.``            ``else``                ``dp[j] = prev[j - 1] + prev[j + 1];``        ``}``    ``}` `    ``// stores the final answer``    ``long` `sum = 0;``    ``for` `(``int` `j = 1; j <= 9; j++)``        ``sum += dp[j];``    ``return` `sum;``}` `// Driver code``static` `void` `Main()``{``    ``int` `n = 2;``    ``Console.WriteLine(answer(n));``}``}` `// This code is contributed by mits`

## PHP

 ``

## Javascript

 ``

Output:

`17`

Time Complexity: O(N)
Auxiliary Space: O(1), since no extra space has been taken.

My Personal Notes arrow_drop_up