Given a length ‘N’, you need to find all the strobogrammatic numbers of length ‘N’.
A strobogrammatic number is a number that looks the same when rotated by 180.
In other words, a number that on rotating right side up and upside down appears the same is a strobogrammatic number.
‘986’ is a strobogrammatic number because on rotating ‘986’ by 180 degrees, ‘986’ will be obtained.

If N = 2, all the strobogrammatic numbers of length = 2 are “11”, “88”, “69”, “96”.
The first line contains an integer ‘T’, denoting the number of test cases.
The first line of each test case contains a single integer denoting ‘N’.
Output Format:
For each test case, print space-separated strings denoting strobogrammatic numbers of the given length.
Print the output of each test case in a separate line.
Note:
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 7
Where ‘T’ is the number of test cases, and ‘N’ is the given length.
Time Limit: 1 sec
2
3
1
101 111 181 609 619 689 808 818 888 906 916 986
0 1 8
Test Case 1: All the possible Strobogrammatic numbers of length = 3 are “101”, “111”, “181”, “609”, “619”, “689”, “808”, “818”, “906”, “916”, “986”.
Test Case 2: Strobogrammatic numbers of length = 1 are “0”, “1”, and “8”.
2
4
2
1001 1111 1691 1881 1961 6009 6119 6699 6889 6969 8008 8118 8698 8888 8968 9006 9116 9696 9886 9966
11 69 88 96
Test Case 1: All the possible Strobogrammatic numbers of length = 4 are printed.
Test Case 2: All the possible Strobogrammatic numbers of length = 2 are printed.
Can you try to divide your problem into smaller sub-problems?
Approach: Out of all the 10 digits, 0,1,6,8,9 will give a valid digit when rotated upside down(top part turned to bottom).
After rotating upside down digits will be-
0 -> 0
1 -> 1
6 -> 9
8 -> 8
9 -> 6
So We have to form numbers using only 0,1,6,8,9.
The basic idea is that we will reduce the problem into small problems. We will recursively solve the problem for length = length - 2. And then add digits out of (0,1,6,8,9) at the starting and the corresponding digits (0,1,9,8,6) at the end.
Recursion will be stopped when len = 0 and len - 1.
If len = 0, we will return an empty string and in case len = 1, we will return three strings “1”, “0”, “8” as these are the strobogrammatic numbers with pen = 1.
Let us understand this with an example for N = 4.
Algorithm:
O(5^N), where ‘N’ is the given length.
Since for every pair of digits in the strobogrammatic number, we have 5 choices(0, 1, 6, 8, 9), and we are iterating over each of them. Therefore, the time complexity is O(5^N).
O(5^N), where ‘N’ is the given length.
Since for every pair of digits in the strobogrammatic number, we have 5 choices(0, 1, 6, 8, 9), and there would be ‘N/2’ pairs. So there will be approximately 5^(N) strobogrammatic numbers of length ‘N’. Therefore, space complexity is O(5^N).