forked from zapellass123/OpenEmailGenerator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBinary Trees With Factors.cpp
36 lines (31 loc) · 1.04 KB
/
Binary Trees With Factors.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public:
int numFactoredBinaryTrees(vector<int>& arr) {
map<int, long long int> m;
sort(arr.begin(), arr.end());
for(int i=0; i<arr.size(); i++){
m.insert({arr[i], 1});
}
for(int i=1; i<arr.size(); i++){
auto it3 = m.find(arr[i]);
long long int count = 0;
for(int j=0; j<i; j++){
if(arr[i]%arr[j]==0)
{
auto it = m.find(arr[i]/arr[j]);
auto it2 = m.find(arr[j]);
if(it!=m.end()) //if it present in the map
{
count += (it->second)*(it2->second); // numbers of ways
}
}
}
it3->second += count;
}
long long int sum = 0;
for(auto it4 = m.begin(); it4!=m.end(); it4++){
sum += it4->second;
}
return sum%(1000000007);
}
};