254. Factor Combinations
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for all possible combinations of factors for a given integer n. The factors must be in the range [2, n - 1]. This implies that 1 and n itself are not part of the factors, and a combination cannot be just [n]. For example, for n=12, valid combinations are [2, 6], [3, 4], and [2, 2, 3]. This is a classic combinatorial problem that is well-suited for a backtracking approach.
Solution
Factor Combinations in Python
from typing import List
class Solution:
def getFactors(self, n: int) -> List[List[int]]:
# Factors must be in the range [2, n-1]. For n <= 3, this is not possible.
if n <= 3:
return []
result = []
path = []
def backtrack(num, start):
# Iterate through potential factors `i` for the current number `num`.
# The loop for `i` goes from `start` up to `sqrt(num)` as an optimization.
i = start
while i * i <= num:
if num % i == 0:
j = num // i
# Combination found: current path + {i, j}
result.append(path + [i, j])
# Recursively find more factors for j.
# The new start is i to maintain non-decreasing order of factors.
path.append(i)
backtrack(j, i)
path.pop() # Backtrack
i += 1
backtrack(n, 2)
return result
Factor Combinations in Java
class Solution {
public List<List<Integer>> getFactors(int n) {
List<List<Integer>> result = new ArrayList<>();
// Factors must be in the range [2, n-1]. For n <= 3, this is not possible.
if (n <= 3) {
return result;
}
backtrack(n, 2, new ArrayList<>(), result);
return result;
}
private void backtrack(int n, int start, List<Integer> path, List<List<Integer>> result) {
// Iterate through potential factors `i` for the current number `n`.
// The loop for `i` goes from `start` up to `sqrt(n)` as an optimization.
for (int i = start; (long)i * i <= n; i++) {
if (n % i == 0) {
int j = n / i;
// Combination found: current path + {i, j}
path.add(i);
path.add(j);
result.add(new ArrayList<>(path));
path.remove(path.size() - 1); // Backtrack j
// Recursively find more factors for j.
// The new start is i to maintain non-decreasing order of factors.
backtrack(j, i, path, result);
path.remove(path.size() - 1); // Backtrack i
}
}
}
}
Factor Combinations in C++
class Solution {
public:
vector<vector<int>> getFactors(int n) {
vector<vector<int>> result;
// Factors must be in the range [2, n-1]. For n <= 3, this is not possible.
if (n <= 3) {
return result;
}
vector<int> path;
backtrack(n, 2, path, result);
return result;
}
private:
void backtrack(int n, int start, vector<int>& path, vector<vector<int>>& result) {
// Iterate through potential factors `i` for the current number `n`.
// The loop for `i` goes from `start` up to `sqrt(n)` as an optimization.
for (int i = start; (long long)i * i <= n; ++i) {
if (n % i == 0) {
int j = n / i;
// Combination found: current path + {i, j}
path.push_back(i);
path.push_back(j);
result.push_back(path);
path.pop_back(); // Backtrack j
// Recursively find more factors for j.
// The new start is i to maintain non-decreasing order of factors.
backtrack(j, i, path, result);
path.pop_back(); // Backtrack i
}
}
}
};
Factor Combinations in TypeScript
function getFactors(n: number): number[][] {
const result: number[][] = [];
// Factors must be in the range [2, n-1]. For n <= 3, this is not possible.
if (n <= 3) {
return result;
}
function backtrack(num: number, start: number, path: number[]): void {
// Iterate through potential factors `i` for the current number `num`.
// The loop for `i` goes from `start` up to `sqrt(num)` as an optimization.
for (let i = start; i * i <= num; i++) {
if (num % i === 0) {
const j = num / i;
// Combination found: current path + {i, j}
const newCombination = [...path, i, j];
result.push(newCombination);
// Recursively find more factors for j.
// The new start is i to maintain non-decreasing order of factors.
path.push(i);
backtrack(j, i, path);
path.pop(); // Backtrack
}
}
}
backtrack(n, 2, []);
return result;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.