Problem of the day
You are given a positive integer ‘N’. Find all the unique combinations of factors of the given number ‘N’. The product of the factors in each combination should be ‘N’.
You should return a list of combinations of factors. In each combination, factors must be sorted in non-decreasing order. All combinations must be placed in lexicographical order in the list. See the example for more clarity.
Note
1. Factors should be strictly greater than 1 and strictly less than ‘N’.
2. If there is no such possible combination of factors, then return an empty list.
Example:
Consider the positive integer ‘N’ = 12.
Then, we can observe that -:
12 = 2 * 2 * 3
12 = 2 * 6
12 = 3 * 4
i.e, possible combinations of factors are [2, 2, 3], [2, 6], [3, 4].
Thus, we should return list [[2,2,3], [2,6], [3, 4]]. Note that in this list all combinations are sorted in non-decreasing order, and all the combinations in the list are placed in the lexicographical order.
The first line of input contains an integer ‘T’ denoting the number of test cases. then ‘T’ test cases follow.
The first and the only line of each test case consist of a single integer ‘N’.
Output format :
For each test case, if there are ‘K’ such possible combinations of factors, then in the first line of the output of the test case print a single integer ‘K’, and then print ‘K’ lines each of them represents a combination of factors of a given integer in non-decreasing order.
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
2 <= N <= 1000
Where ‘T’ is the total number of test cases, and ‘N’ is the given integer.
Time limit: 1 sec
2
5
12
0
3
2 2 3
2 6
3 4
Test case 1:
The integer 5 is a prime number, and it has no factors. Note, we consider that factors should be strictly greater than 1 and strictly less than ‘N’.
Test case 2:
See the problem statement for an explanation.
2
16
4
4
2 2 2 2
2 2 4
2 8
4 4
1
2 2
Incrementally find each combination using backtracking.
We will make a 2D list of integers ‘result’ to store all the unique factor combinations possible for the given integer ‘N’. We recursively build each of the combinations of the factors in lexicographical order and append them in the list result.
Algorithm
O(N ^ (log(N) / 3)), where ‘N’ is the given integer.
For integer ‘N’ the number of its divisors is of the order of cube root of ‘N’ ie O(N ^ ⅓).
It implies that in each recursive step we can have at most N ^ ⅓ further recursive calls. Also, recursion depth is of the order of log(N) because the maximum size of any required combination of factors of integer ‘N’ can be at most log(N). Thus overall complexity will be O(N ^ (log(N) / 3)).
O(log(N)), where ‘N’ is the given integer.
The recursion depth is of the order of log(N) because the maximum size of any required combination of factors of integer ‘N’ can be at most log(N). So, the space used by the recursion stack is of the order of log(N). Thus, overall space complexity will be O(log(N)).