



Return the solution modulo 10^9 + 7.
You are given ‘N’ = 2, all the unique 2 digit numbers formed moving the knight on the number pad are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]. Hence the answer is 20.
The first line of input contains a single integer ‘T’ representing the number of test cases.
The first and the only line of each test case contains a single integer ‘N’.
Print a single integer for each test case, representing the number of distinct numbers formed by moving the knight.
Print a separate line for each test case.
1 <= T <= 10
1 <= N <= 10^3
Time Limit: 1 sec
You do not need to print anything. It has already been taken care of. Just implement the given function.
In this approach, we will visit each position in the number pad and count the number of distinct numbers that can be dialled. We will make a map of all the places to be visited from a given number.
We create a recursive function helper(currPos, nextPositions, digitsRem), currPos is the current key on the number pad, nextPositions is the map of the next positions from any key, and digitsRem is the number of digits left to form the number.
This approach is similar to the previous approach, but we will make a cache to store the already calculate solutions of the current position and the number of digits remaining.
We create a recursive function helper(currPos, nextPositions, digitsRem, cache), currPos is the current key on the number pad, nextPositions is the map of the next positions from any key, digitsRem is the number of digits left to form the number and cache is the cache that stores previously calculated answers.
In this approach, we can see that the number of ways to reach a given position in the number pad, is the sum of the number of ways to reach all the positions from which we can come to our current position. For example, the number of ways to reach 1 is the sum of the number of ways to reach 6 and 8.
So to find the number of distinct N digit numbers we can start with the number of ways to reach each key in the number pad as one. Then calculate the number of ways for each key N - 1 times.