823. Binary Trees With Factors in C++ [Dynamic Programming]
\nclass Solution {\npublic:\n\t//given in problem\n\tconst int mod = 1e9 + 7;\n\tint numFactoredBinaryTrees(vector<int> &arr)\n\t{\n\t\t//define n as zize of ar
```\nclass Solution {\npublic:\n\t//given in problem\n\tconst int mod = 1e9 + 7;\n\tint numFactoredBinaryTrees(vector<int> &arr)\n\t{\n\t\t//define n as zize of array\n\t\tint n = arr.size();\n\t\tvector < long long > dp(n, 1);\n\t\tunordered_map<int, int> mp;\n\t\tsort(arr.begin(), arr.end());\n\t\t//iterate the loop\n\t\tfor (int i = 0; i < n; i++)\n\t\t{\n\t\t\t//store the index of various elements of the array\n\t\t\tmp[arr[i]] = i;\n\t\t\tfor (int j = i - 1; j >= 0; j--)\n\t\t\t{\n\t\t\t\t//to check whether arr[j] is a factor of arr[i] or not\n\t\t\t\tif (arr[i] % arr[j] == 0)\n\t\t\t\t{\n\t\t\t\t\t//store to temp\n\t\t\t\t\tint temp = arr[i] / arr[j];\n\t\t\t\t\t//now check if count of temp in map exists then\n\t\t\t\t\tif (mp.count(temp))\n\t\t\t\t\t{\n\t\t\t\t\t\t//check the presence of the required no in map,\n\t\t\t\t\t\t//If it is in the map then increase dp[i] by the product of the dp[j]\n\t\t\t\t\t\t//dp[mp[temp]] taking the modulus\n\t\t\t\t\t\tdp[i] += (dp[j] * dp[mp[temp]]) % mod;\n\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t}\n\t\tlong long ans = 0;\n\t\tans = accumulate(dp.begin(), dp.end(), 0LL);\n\t\t//return total sum with mod\n\t\treturn (ans % mod);\n\t}\n};\n\n```
['Dynamic Programming', 'C']