Logo
Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Solution: Factor Combinations

Problem Statement

Numbers can be regarded as the product of their factors.

For example, 8 = 2 x 2 x 2 = 2 x 4.

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

Example 1:

Input: n = 8  
Output: [[2, 2, 2], [2, 4]]   

Example 2:

Input: n = 20  
Output: [[2, 2, 5], [2, 10], [4, 5]]  

Constraints:

  • 1 <= n <= 10<sup>7</sup>

Solution

We can use backtracking to find all the factors of a given number n.

.....

.....

.....

Like the course? Get enrolled and start learning!