Java Solution easy to understand | Hapmap dp | 38 ms solution | inline comments
Java Hapmap | 38ms solution\n\n\n\n public int numFactoredBinaryTrees(int[] arr) {\n        //sort the arr first \n        int MOD=1000000007;//get mod because 
**Java Hapmap | 38ms solution**\n\n\n```\n public int numFactoredBinaryTrees(int[] arr) {\n        //sort the arr first \n        int MOD=1000000007;//get mod because of ans to long\n         Arrays.sort(arr);\n        HashMap<Integer,Long>map=new HashMap<Integer,Long>(); // declare map to keep track of arr value and they sum \n        long ans=1;\n        map.put(arr[0],ans); //key=2,vaule=1;\n        for(int i=1;i<arr.length;i++)//i=1\n        {\n            long sum=1;\n            for(int j=0;j<i;j++)//j=0\n            {\n                if(arr[i]%arr[j]==0 &&map.containsKey(arr[i]/arr[j]))\n                    //(4%2==0&&keyinmap(4/2=2))ture\n                    sum+=(map.get(arr[i]/arr[j])*map.get(arr[j]));//sum=(1*1)+1=2\n            }\n            map.put(arr[i],sum);//key=4,2\n            ans+=sum;//3\n        }\n        return (int)(ans%MOD);//3%MOD\n\t\t}\n```\n**TC : O(N^2)**\n**SC: O(N)**
['Dynamic Programming', 'Java']