JAVA SOLUTION -> USING SORTING AND DP
Main Points :\nSort the array , as we have to find for each element present in arr[i] , can we make it a parent node by multplying its children , basically does
Main Points :\nSort the array , as we have to find for each element present in arr[i] , **can we make it a parent node by multplying its children** , basically does **both factors of a element are present in order to make it a parent node.**\nIf we find that the factors are indeed present **we further have to multiply the number of ways of both children** ,in which these children can be formed if they are present as parent node.\nso for each element we store the number of ways in which it can be a parent element in a dp array.\n```\nclass Solution {\n    public int numFactoredBinaryTrees(int[] arr) {\n        \n        int n = arr.length;\n        Arrays.sort(arr);\n        \n        long trees = 0;\n        long mod = (long)1e9+7;\n\t\t// making a dp array to store at each point the numbers of ways to make a tree if the current element is taken as parent node\n        long []dp = new long[n];\n        \n        for( int i=0; i<n; i++ ){\n\t\t\t// current trees initialized as 1 , as a number can always make a single by itself\n            long curr = 1;\n            \n            int lo = 0;\n            int hi = i-1;\n            \n\t\t\t// two pointers approach to find the factors of current element and if both factors are present in the array\n            while( lo <= hi ){\n                \n                long prod = arr[lo] * arr[hi];\n\t\t\t\t// check to find factors and second check to make sure that the number is valid ( i did this check as i was encountering a problem in a testcase even though the numbers were big their product was small and coming out to be exact as current element)\n                if( prod == ( arr[i] % mod ) && arr[lo] <= ( arr[i]/2 ) && arr[hi] <= ( arr[i]/2 ) ){\n                    int diff = 2;\n\t\t\t\t\t// if both the numbers are same there is only one way we can arrange as either being on left or right is same for them\n                    if( arr[lo] == arr[hi] ) diff = 1;\n                    curr = ( curr +  ( ( ( dp[hi] * dp[lo] ) % mod ) * diff ) % mod ) % mod;\n                    lo++;\n                    hi--;\n                }\n                else if ( prod > arr[i] ) hi--;\n                else lo++;\n            }\n            // storing the current trees\n            dp[i] = curr;\n\t\t\t// storing total trees we can make\n            trees = ( trees + curr )%mod;\n        }\n        \n        return (int)trees;\n    }\n}\n```\n
['Dynamic Programming', 'Sorting', 'Java']