Given two integers n and k, split the number n into exactly k positive integers such that the product of these integers is equal to n.
Return any one split in which the maximum difference between any two numbers is minimized. You may return the result in any order.
Example 1:
Input: n = 100, k = 2
Output: [10,10]
Explanation:
The split [10, 10] yields 10 * 10 = 100 and a max-min difference of 0, which is minimal.
Example 2:
Input: n = 44, k = 3
Output: [2,2,11]
Explanation:
[1, 1, 44] yields a difference of 43[1, 2, 22] yields a difference of 21[1, 4, 11] yields a difference of 10[2, 2, 11] yields a difference of 9Therefore, [2, 2, 11] is the optimal split with the smallest difference 9.
Constraints:
4 <= n <= 1052 <= k <= 5k is strictly less than the total number of positive divisors of n.Problem Overview: You are given an integer n and a target count k. The task is to decompose n into exactly k integer factors whose product equals n while keeping the factors as balanced as possible (no unnecessary skew toward very small or very large factors).
Approach 1: Exhaustive Factor Combination (Brute Force) (Time: O(d^k), Space: O(k))
Start by generating all divisors of n. Then try every possible combination of k divisors whose product equals n. A recursive search multiplies chosen factors and tracks how many have been used so far. If the product exceeds n or the count exceeds k, the branch stops early. This method is straightforward but explores many redundant combinations, especially when n has many divisors.
Approach 2: Backtracking with Ordered Factors and Pruning (Time: O(d^k) worst case, typically much smaller; Space: O(k))
A better strategy uses backtracking combined with properties from number theory. Instead of trying all divisor permutations, enforce a non‑decreasing order of factors. At each recursion step, iterate through divisors starting from the previous factor to avoid duplicate arrangements. Divide the remaining product by the chosen factor and continue searching. If the remaining value cannot produce enough factors to reach k, prune the branch immediately.
The key insight is that factor combinations behave like multiplicative partitions. Ordering the factors and shrinking the remaining product drastically reduces the search space. This approach also keeps factors naturally balanced because extremely small factors quickly force large remaining values, which fail the pruning checks.
Precomputing divisors of n in O(√n) time further speeds up the recursion. Each step only explores valid divisors of the remaining number, making the search practical even when the theoretical worst case is exponential.
Recommended for interviews: Interviewers expect the optimized backtracking approach. The brute force solution shows that you understand factor generation, but the ordered recursion with pruning demonstrates deeper understanding of math structure and efficient search space reduction.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Exhaustive Factor Combination | O(d^k) | O(k) | Useful for understanding multiplicative partitions or when n has very few divisors |
| Backtracking with Ordered Factors and Pruning | O(d^k) worst case | O(k) | General solution. Avoids duplicate permutations and prunes impossible branches early |
Balanced K-Factor Decomposition | LeetCode 3669 | Weekly Contest 465 • Sanyam IIT Guwahati • 1,528 views views
Watch 9 more video solutions →Practice Balanced K-Factor Decomposition with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor