# Factor Combinations

Posted: 11 Mar, 2021
Difficulty: Moderate

## PROBLEM STATEMENT

#### 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.
``````
##### Input format:
``````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.
``````
##### Constraints:
``````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
`````` Approach 1

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

• Create a 2D list of integers ‘result’.
• Create a list of integer ‘factors’.
• Create a recursive function factorCombinationsHelper(curNum, start, factors), where ‘curNum’ be the current integer whose factor greater or equal to ‘start’ needs to be appended in list factors, ‘factors is running a combination of factors of ‘N’. In each recursive call, we’ll do the following-:
• If curNum = 1, then append list factors in list result and return.
• Otherwise, Run a loop where ‘i’ ranges from start to N - 1 and in each iteration do the following -:
• If curNum is divisible by ‘i’, then append ‘i’ in the list factors, then call factorCombinationsHelper(curNum/i, i, factors). After that, we remove the last integer i.e this ‘i’ from list factors, in order to find combinations excluding this one occurrence of factor ‘i’.
• Return list result by calling this recursive function as factorCombinationsHelper(N, 2, factors).