Quick Overview

Return every way to write an integer as a product of factors greater than 1 and smaller than the number itself, each list in non-decreasing order and the lists sorted. Tests backtracking with a minimum-factor bound, pruning at the square root and avoiding duplicate orderings.

List All Factorizations of an Integer into Factors Greater Than 1

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Numbers can be regarded as the product of their factors. For example, `8 = 2 x 2 x 2 = 2 x 4`. Write a function that takes an integer `n` and returns all possible combinations of its factors, where every factor is in the range `[2, n - 1]`. ### Function Signature ```python def get_factors(n: int) -> list[list[int]]: ``` ### Rules - Each combination lists its factors in non-decreasing order. `[2, 6]` and `[6, 2]` are the same combination, and only `[2, 6]` appears in the output. - Because every factor is at most `n - 1`, a combination always has at least two factors. `[n]` on its own is not a combination. - Return the combinations sorted in ascending lexicographic order, comparing element by element (the order Python's `sorted` gives for lists of integers). - When `n` has no such combination (`n` is 1 or a prime), return an empty list. ### Constraints - `1 <= n <= 10^7` ### Examples ```text Input: n = 12 Output: [[2, 2, 3], [2, 6], [3, 4]] ``` 12 can be written as `2 x 2 x 3`, `2 x 6` and `3 x 4`, and no other non-decreasing product of factors from 2 to 11 equals 12. `[2, 2, 3]` comes before `[2, 6]` because their second elements compare 2 < 6. ```text Input: n = 32 Output: [[2, 2, 2, 2, 2], [2, 2, 2, 4], [2, 2, 8], [2, 4, 4], [2, 16], [4, 8]] ``` 32 is `2^5`, so each combination matches one way to split the exponent 5 into at least two positive parts: 1+1+1+1+1, 1+1+1+2, 1+1+3, 1+2+2, 1+4 and 2+3. Those six splits give the six lists above, shown in lexicographic order. ```text Input: n = 37 Output: [] ``` 37 is prime, so its only factors are 1 and 37, and neither lies in the range 2 to 36.

Overview: Return every way to write an integer as a product of factors greater than 1 and smaller than the number itself, each list in non-decreasing order and the lists sorted. Tests backtracking with a minimum-factor bound, pruning at the square root and avoiding duplicate orderings.

Numbers can be regarded as the product of their factors. For example, `8 = 2 x 2 x 2 = 2 x 4`. Write a function `get_factors(n)` that takes an integer `n` and returns all possible combinations of its factors, where every factor is an integer in the range `[2, n - 1]`. Rules: - Each combination lists its factors in non-decreasing order. `[2, 6]` and `[6, 2]` are the same combination, and only `[2, 6]` appears in the output. - Because every factor is at most `n - 1`, a combination always has at least two factors. `[n]` on its own is not a combination. - Return the combinations sorted in ascending lexicographic order, comparing element by element (the order Python's `sorted` gives for lists of integers). - When `n` has no such combination (`n` is 1 or a prime), return an empty list. Every factor in a combination is at most `n / 2`, so no value exceeds `2^31 - 1` and 32-bit integers are enough in every language. Constraints: - `1 <= n <= 10^7` Example 1: ```text Input: n = 12 Output: [[2, 2, 3], [2, 6], [3, 4]] ``` 12 can be written as `2 x 2 x 3`, `2 x 6` and `3 x 4`, and no other non-decreasing product of factors from 2 to 11 equals 12. `[2, 2, 3]` comes before `[2, 6]` because their second elements compare 2 < 6. Example 2: ```text Input: n = 37 Output: [] ``` 37 is prime, so neither of its factors 1 and 37 lies in the range 2 to 36.

Constraints

  • 1 <= n <= 10^7

Examples

Input: (1,)

Expected Output: []

Explanation: Minimum n = 1 has no factor in range, so the result is empty.

Input: (2,)

Expected Output: []

Explanation: n = 2 is the smallest prime and has no combination.

Hints

  1. Listing each combination in non-decreasing order gives it a single form, so reordered copies such as [6, 2] never appear.
  2. Once the smallest factor f of a combination is fixed, the other factors multiply to n / f and each is at least f.
  3. Try n = 12 by hand: [2, 2, 3] has to come before [2, 6] because the lists are compared element by element.

Factor Combinations in Lexicographic Order

mediumGeneralSoftware Engineer

Numbers can be regarded as the product of their factors. For example, 8 = 2 x 2 x 2 = 2 x 4.

Write a function get_factors(n) that takes an integer n and returns all possible combinations of its factors, where every factor is an integer in the range [2, n - 1].

Rules:

  • Each combination lists its factors in non-decreasing order. [2, 6] and [6, 2] are the same combination, and only [2, 6] appears in the output.
  • Because every factor is at most n - 1, a combination always has at least two factors. [n] on its own is not a combination.
  • Return the combinations sorted in ascending lexicographic order, comparing element by element (the order Python's sorted gives for lists of integers).
  • When n has no such combination (n is 1 or a prime), return an empty list.

Every factor in a combination is at most n / 2, so no value exceeds 2^31 - 1 and 32-bit integers are enough in every language.

Constraints:

  • 1 <= n <= 10^7

Example 1:

Input: n = 12
Output: [[2, 2, 3], [2, 6], [3, 4]]

12 can be written as 2 x 2 x 3, 2 x 6 and 3 x 4, and no other non-decreasing product of factors from 2 to 11 equals 12. [2, 2, 3] comes before [2, 6] because their second elements compare 2 < 6.

Example 2:

Input: n = 37
Output: []

37 is prime, so neither of its factors 1 and 37 lies in the range 2 to 36.

Examples

Example 1
Input
(1,)
Output
[]
Notes
Minimum n = 1 has no factor in range, so the result is empty.
Example 2
Input
(2,)
Output
[]
Notes
n = 2 is the smallest prime and has no combination.

Constraints

  • •1 <= n <= 10^7

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def get_factors(n: int) -> list[list[int]]:
Sample call
get_factors(1)
Language
Case 1
Input
(1,)
Expected
[]
Case 2
Input
(2,)
Expected
[]

Browse More Questions

Show the approach

Approach

Collect the divisors of n that lie in the range 2 to sqrt(n), in increasing order. Any factor that can open the rest of a combination is one of them: it divides the value r still to be factored, r divides n, and the factor squared is at most r, which is at most n.

A depth-first search keeps the factors chosen so far in a path, the value r still to be factored, and the index of the smallest divisor allowed next. It walks the allowed divisors d in increasing order and stops once d * d > r. When d divides r it pushes d, recurses on r / d starting from the same index, records path + [r / d], and pops d.

Every recorded list is non-decreasing, because later factors start at d and r / d >= d whenever d * d <= r. Every combination is found: in a non-decreasing combination f1 <= f2 <= ... <= fm with m >= 2, f1 * f1 <= f1 * f2 <= n, so the root tries f1. Either m = 2 and the pair is recorded, or f2, ..., fm is a combination of n / f1 whose factors are at least f1, and induction applies. Each combination comes from exactly one sequence of choices, so none repeats, and [n] alone is never recorded because a factor is always pushed before anything is recorded.

The lists come out already sorted, so no final sort is needed. Under a fixed path and divisor d, each list from the recursion continues with some e where e * e <= r / d, so e < r / d, and it sorts before path + [d, r / d], which is recorded right after the recursion returns. Lists that use a smaller d come before lists that use a larger d.

For n = 1, n = 2 and every prime the divisor list is empty, so the result is []. Perfect squares such as 49 need the comparison d * d <= r with equality allowed, or [7, 7] is lost. Every output value is at most n / 2, so 32-bit integers hold it; Java and C++ compute d * d in 64 bits as a precaution.

Time complexity:
O(sqrt(n) + F * (D + log n)), where F is the number of combinations and D is the number of divisors of n up to sqrt(n)
Space complexity:
O(F * log n) for the output plus O(D + log n) for the divisor list and the recursion