Skip to the code
+25 XP

#306 Factor Grid Layouts

medium

A layout engine can nest grids: a page of n tiles might be 2 groups of 6, or 2 groups of 2 groups of 3. Each nesting is a way to write n as a product of two or more whole numbers, each at least 2.

Write factorLayouts(n) that returns every such product as an array of factors in non-decreasing order. List the arrays in sorted order: compare first factors, then second factors, and so on.

If n can't be written this way (for example 1 or a prime), return [].

Example 1

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

12 = 2 × 2 × 3 = 2 × 6 = 3 × 4. 12 alone doesn't count, since it needs two or more factors.

Example 2

Input: n = 7
Output: []

7 is prime, so it can't be split into smaller factors.

Constraints

  • 1 <= n <= 10^6
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).