Java short and crisp | using DP & Two Pointer Approach
\nclass Solution {\n    public int numFactoredBinaryTrees(int[] arr) {\n        Arrays.sort(arr);\n        int n = arr.length ,mod = 1_000_000_007;\n        lon
```\nclass Solution {\n    public int numFactoredBinaryTrees(int[] arr) {\n        Arrays.sort(arr);\n        int n = arr.length ,mod = 1_000_000_007;\n        long res=0, ways[] = new long[n];\n        \n        for(int i=0,l=0,r=0 ;i<n; i++){\n            ways[i] = 1;\n            //Two-poitner Approach\n            l = 0; r = i-1;\n            while(l <= r){\n\t\t\t/*Instead of checking arr[l]*arr[r] == arr[i], to prevent overflow\n\t\t\tuse arr[l] == arr[i]/arr[r]. */\n                if( arr[l]*1.0 == arr[i]*1.0/arr[r]){         \n                    ways[i] += ways[l]*ways[r]*( l==r ? 1 : 2 );\n                    l++;r--;\n                }\n                else if ( arr[l]*1.0 > arr[i]*1.0/arr[r]) r--;\n                else l++;\n            }\n            res = (res+ways[i]) % mod;\n        }\n        \n        return (int)res;\n    }\n}\n\n```
['Two Pointers', 'Dynamic Programming']