Factor Combinations - Problem

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

Given an integer n, return all possible combinations of its factors. You may return the answer in any order.

Note: The factors should be in the range [2, n - 1].

Input & Output

Example 1 — Basic Case
$ Input: n = 12
Output: [[2,6],[2,2,3],[3,4]]
💡 Note: 12 can be factored as 2×6, 2×2×3, or 3×4. All factors must be in range [2, 11]
Example 2 — Single Factor Pair
$ Input: n = 8
Output: [[2,4],[2,2,2]]
💡 Note: 8 can be factored as 2×4 or 2×2×2
Example 3 — Prime Number
$ Input: n = 37
Output: []
💡 Note: 37 is prime, so it has no factors in range [2, 36]

Constraints

  • 1 ≤ n ≤ 107
  • Factors must be in range [2, n-1]

Visualization

Tap to expand
Factor Combinations INPUT n = 12 Integer to factorize Possible Factors 2 3 4 6 Range: [2, n-1] = [2, 11] 12 divides by: 12/2=6, 12/3=4, 12/4=3 12/6=2 Constraints: - Factors in [2, n-1] - Product equals n ALGORITHM STEPS 1 Start Backtracking Begin with n=12, start=2 2 Find Divisors Loop i from start to sqrt(n) 3 Recurse If n%i==0, add i, recurse n/i 4 Collect Results Store valid combinations Recursion Tree: 12 2,6 3,4 2,2,3 2,2,3 FINAL RESULT All Factor Combinations: [2, 6] 2 x 6 = 12 OK [2, 2, 3] 2 x 2 x 3 = 12 OK [3, 4] 3 x 4 = 12 OK Output: [[2,6],[2,2,3],[3,4]] Complexity: Time: O(sqrt(n)^k) Space: O(log n) recursion Key Insight: Use backtracking with pruning: iterate factors from 'start' to sqrt(n) to avoid duplicates. For each valid divisor i, recursively find factors of n/i with start=i (ensures non-decreasing order). This prevents generating [3,4] and [4,3] separately, keeping only sorted combinations. TutorialsPoint - Factor Combinations | Optimal Backtracking Solution
Asked in
Google 15 Facebook 12 Amazon 8
23.0K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen