FastPrepFactor Combinations

Factor Combinations

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given an integer n, return every unique combination of integers from 2 through n whose product is n.

Do not include the one-element combination [n]. Within each combination, factors must be in nondecreasing order. Return all combinations in lexicographic order.

Function

getFactors(n: int) → int[][]

Examples

Example 1

n = 12return = [[2,2,3],[2,6],[3,4]]

The three nontrivial nondecreasing factorizations of 12 are 2×2×3, 2×6, and 3×4.

Example 2

n = 16return = [[2,2,2,2],[2,2,4],[2,8],[4,4]]

Nondecreasing order removes factor permutations.

Example 3

n = 37return = []

A prime has no nontrivial factor combination.

Constraints

  • 2 <= n <= 10000000.
  • Every returned factor is between 2 and n - 1.
  • The total result fits in memory.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int[][] getFactors(int n) {
    // Write your code here.
}
n12
expected[[2,2,3],[2,6],[3,4]]
Checking account…