Binary Trees With Factors - Problem

Imagine you're a tree architect with a unique challenge! You have an array of unique integers arr, where each number is greater than 1. Your task is to build special binary trees where:

  • Each non-leaf node's value equals the product of its two children's values
  • You can use each number from the array multiple times
  • All values in the tree must come from your given array

For example, with [2, 4, 5, 10]:

  • You can build a tree with root 10 and children 2 and 5 (since 2 ร— 5 = 10)
  • You can build a tree with root 4 and children both 2 (since 2 ร— 2 = 4)

Goal: Count how many different binary trees you can construct following these rules. Since the answer can be huge, return it modulo 109 + 7.

Input & Output

example_1.py โ€” Python
$ Input: [2, 4]
โ€บ Output: 3
๐Ÿ’ก Note: We can make these trees: [2], [4], and [4] with children [2, 2]. The root 4 has two children 2 and 2, and 2 ร— 2 = 4.
example_2.py โ€” Python
$ Input: [2, 4, 5, 10]
โ€บ Output: 7
๐Ÿ’ก Note: We can make trees: [2], [4], [5], [10], [4 with children 2,2], [10 with children 2,5], and [10 with children 5,2]. Note that [10 with children 2,5] and [10 with children 5,2] are different trees.
example_3.py โ€” Python
$ Input: [3, 6]
โ€บ Output: 2
๐Ÿ’ก Note: We can only make single-node trees [3] and [6]. We cannot make [6] with children [3,2] because 2 is not in our array.

Visualization

Tap to expand
๐Ÿญ Tree Factory Assembly LineRaw Materials (Sorted)24510Assembly StationsStation 12ร—2โ†’4Output: 1Station 22ร—5โ†’10Output: 1Station 35ร—2โ†’10Output: 1Production SummarySingle Products (Standalone):โ€ข Tree [2] = 1 wayโ€ข Tree [4] = 1 wayโ€ข Tree [5] = 1 wayโ€ข Tree [10] = 1 wayTotal: 4 + 3 = 7 configurationsDynamic Programming ProcessAlgorithm Steps:1. Sort array: [2, 4, 5, 10]2. For each element, check all smaller pairs as factors3. dp[i] = 1 + ฮฃ(dp[left] ร— dp[right]) for valid pairsโšก Time Complexity: O(nยฒ)๐ŸŽฏ Key Insight: Count trees by combining results from smaller subproblems
Understanding the Visualization
1
Inventory Check
Sort available components [2,4,5,10] by size
2
Single Components
Each component can be a standalone product (4 ways)
3
Assembly Combinations
Find valid combinations: 4=2ร—2, 10=2ร—5, 10=5ร—2
4
Count Total Products
Sum all possible assembly configurations = 7 total
Key Takeaway
๐ŸŽฏ Key Insight: Use dynamic programming to avoid recalculating the same subtrees. For each number, the total trees equals 1 (single node) plus the sum of products of subtree counts for all valid factor pairs.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

Sorting takes O(n log n), then for each of n elements, we check all previous elements for factor pairs

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

DP array to store results for each element

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค arr.length โ‰ค 1000
  • 2 โ‰ค arr[i] โ‰ค 109
  • All values in arr are distinct
  • Answer is guaranteed to fit in a 32-bit signed integer after taking modulo 109 + 7
Asked in
Google 15 Amazon 8 Facebook 12 Microsoft 6
73.9K Views
Medium Frequency
~22 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen