FAQs about Binary Trees with Factors.
Providing you some of the Frequently Asked Questions for this question.\nI arranged the questions from less to more (frequency).\nIf you don\'t know about the s
Providing you some of the *Frequently Asked Questions* for this question.\nI arranged the questions from less to more (frequency).\nIf you don\'t know about the solution please have a look at it before reading FAQs. I have attached it below!\n\n**Why hashmap?**\nBecause we need to store count of trees which can be made with corresponding values of array.\n\n**Why count is initially mapped as 1?**\nSince single element can make a tree!\n\n**Why sorted? (Most Frequently Asked Question)**\nInorder to understand this answer, we need to look at another question (Follow up)\n\n**`Why it is A[i] % rootEle == 0 && rootWithCount.find(A[i] / rootEle) != rootWithCount.end() ?`**\n**`Why  A[i] % rootEle == 0 ? `**\n We have to check whether any rootEle (previously stored value in hashmap) is a factor of A[i]. \n As per the question, For a element to be valid root, we should have two childrens whose product is equal to root.\n \n*If we need to get element by multipling any two numbers, atleast one of it has to be its factor.*\n **Example: Root -> 10; Childrens -> 5, 2 Both 5 and 2 are factors.**\n \n**` Why rootWithCount.find(A[i] / rootEle) != rootWithCount.end() ?`**\n We found rootEle as a factor, is there are any element in the map, so that they multiplies to give the root value.\n \n Now coming back to our question (why sorted)\n \n **For every number, their factors will be less than it**. SIMPLE SO SORTED.\n\n\n**Upvote. It would be encouraging.** Correct me if am wrong!\n\nHere is my C++ version of @Self_Learner Solution!\n\n```\nclass Solution {\npublic:\n    int mod = pow(10,9) + 7;\n    int numFactoredBinaryTrees(vector<int>& A) {\n        sort(A.begin(), A.end());\n        unordered_map <int, long> rootWithCount;\n        \n        rootWithCount[A[0]] = 1;\n        for(int i = 1; i < A.size(); i++){\n            long count = 1;\n            for(auto j : rootWithCount){\n                int rootEle = j.first;\n                if(A[i] % rootEle == 0 && rootWithCount.find(A[i] / rootEle) != rootWithCount.end()){\n                    count += rootWithCount[rootEle] * rootWithCount[A[i] / rootEle];\n                    \n                }\n            }\n            rootWithCount[A[i]] = count;\n        }\n        int noOfTrees = 0;\n        for(auto i : rootWithCount){\n            noOfTrees = (noOfTrees + i.second) % mod;\n        }\n        return noOfTrees;\n    }\n};\n```
['C', 'C++']