Skip to content

Commit b242a7d

Browse files
authored
Merge pull request #3721 from nishant0708/not_counting
Point_not_counted
2 parents 8e37728 + 33ce0e8 commit b242a7d

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
id: binary-trees-with-factors
3+
title: Binary Trees With Factors
4+
sidebar_label: 0823-Binary-Trees-With-Factors
5+
tags:
6+
- Dynamic Programming
7+
- Trees
8+
- Hashing
9+
- C++
10+
- Java
11+
- Python
12+
description: "This document provides a solution to the Binary Trees With Factors problem, where we need to count the number of binary trees that can be formed with a given array of integers such that each node's value is the product of its children's values."
13+
---
14+
15+
## Problem
16+
17+
Given an array of unique integers, `arr`, where each integer `arr[i]` is greater than 1, we need to return the number of binary trees we can make. Each non-leaf node's value should be equal to the product of its children's values.
18+
19+
The answer may be too large, so return it modulo `10^9 + 7`.
20+
21+
### Examples
22+
23+
**Example 1:**
24+
25+
Input: arr = [2, 4]
26+
Output: 3
27+
Explanation: We can make these trees: [2], [4], [4, 2, 2]
28+
29+
**Example 2:**
30+
31+
Input: arr = [2, 4, 5, 10]
32+
Output: 7
33+
Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2]
34+
35+
### Constraints
36+
37+
- `1 <= arr.length <= 1000`
38+
- `2 <= arr[i] <= 10^9`
39+
- All the values of `arr` are unique.
40+
41+
### Approach
42+
43+
To solve this problem, we can use dynamic programming and a hash map to store the number of trees that can be formed with each value as the root. The steps are as follows:
44+
45+
1. Sort the array to ensure that we consider smaller values first.
46+
2. Use a hash map to store the number of ways to form a tree with each value.
47+
3. Iterate through each value in the sorted array, and for each value, iterate through all previously seen values. If the current value can be divided by one of the previously seen values, and the result is also in the array, update the count of trees for the current value.
48+
49+
### Solution
50+
51+
#### Code in Different Languages
52+
53+
### C++ Solution
54+
```cpp
55+
#include <vector>
56+
#include <unordered_map>
57+
#include <algorithm>
58+
59+
using namespace std;
60+
61+
class Solution {
62+
public:
63+
int numFactoredBinaryTrees(vector<int>& arr) {
64+
sort(arr.begin(), arr.end());
65+
unordered_map<int, long> dp;
66+
long result = 0, mod = 1e9 + 7;
67+
68+
for (int i = 0; i < arr.size(); ++i) {
69+
dp[arr[i]] = 1;
70+
for (int j = 0; j < i; ++j) {
71+
if (arr[i] % arr[j] == 0 && dp.count(arr[i] / arr[j])) {
72+
dp[arr[i]] = (dp[arr[i]] + dp[arr[j]] * dp[arr[i] / arr[j]]) % mod;
73+
}
74+
}
75+
result = (result + dp[arr[i]]) % mod;
76+
}
77+
78+
return result;
79+
}
80+
};
81+
```
82+
### Java Solution
83+
84+
```java
85+
import java.util.Arrays;
86+
import java.util.HashMap;
87+
import java.util.Map;
88+
89+
public class Solution {
90+
public int numFactoredBinaryTrees(int[] arr) {
91+
Arrays.sort(arr);
92+
Map<Integer, Long> dp = new HashMap<>();
93+
long result = 0, mod = 1_000_000_007;
94+
95+
for (int i = 0; i < arr.length; ++i) {
96+
dp.put(arr[i], 1L);
97+
for (int j = 0; j < i; ++j) {
98+
if (arr[i] % arr[j] == 0 && dp.containsKey(arr[i] / arr[j])) {
99+
dp.put(arr[i], (dp.get(arr[i]) + dp.get(arr[j]) * dp.get(arr[i] / arr[j])) % mod);
100+
}
101+
}
102+
result = (result + dp.get(arr[i])) % mod;
103+
}
104+
105+
return (int) result;
106+
}
107+
}
108+
```
109+
### Python Solution
110+
111+
```python
112+
def numFactoredBinaryTrees(arr):
113+
arr.sort()
114+
dp = {}
115+
mod = 10**9 + 7
116+
117+
for num in arr:
118+
dp[num] = 1
119+
for prev in dp:
120+
if num % prev == 0 and num // prev in dp:
121+
dp[num] = (dp[num] + dp[prev] * dp[num // prev]) % mod
122+
123+
return sum(dp.values()) % mod
124+
125+
# Test
126+
arr = [2, 4, 5, 10]
127+
print(numFactoredBinaryTrees(arr)) # Output: 7
128+
```
129+
130+
### Complexity Analysis
131+
**Time Complexity:** O(n^2)
132+
133+
>Reason: We use two nested loops to check pairs of numbers and update the count of trees.
134+
135+
**Space Complexity:** O(n)
136+
137+
>Reason: We use a dictionary to store the number of ways to form a tree with each value.
138+
139+
This solution efficiently counts the number of binary trees that can be formed using the given array by utilizing dynamic programming and a hash map. The time complexity is quadratic, and the space complexity is linear, making it suitable for the problem constraints.
140+
141+
### References
142+
**LeetCode Problem:** Binary Trees With Factors
143+

0 commit comments

Comments
 (0)