Find all divisors
Intuition\nBasic intution behind the solution is that maximum sum that can be formed would be 5021e5 sou there would be less number of divisors for this.\n\n# A
# Intuition\nBasic intution behind the solution is that maximum sum that can be formed would be $$50*2*1e5$$ sou there would be less number of divisors for this.\n\n# Approach\nFind if we can create subtrees with a particular sum.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n\n    void findfactors(vector<int>&factors, int sum)\n    {\n        for(int i=1; i<=sqrt(sum); i++)\n        {\n            if(sum%i==0)\n            {\n                factors.push_back(i);\n                if(i!=sum/i) factors.push_back(sum/i);\n            }\n        }\n    }\n\n    int dfs(int node, int par, vector<int>v[], int divs, vector<int>&nums)\n    {\n        int sum=nums[node];\n        bool flag = true;\n        for(int i=0; i<v[node].size(); i++)\n        {\n            int child=v[node][i];\n            if(child!=par)\n            {\n                int z1=dfs(child,node,v,divs,nums);\n                sum+=z1;\n                if(z1==-1) flag=false;\n            }\n        }\n        if(flag==false || sum>divs) return -1;\n        if(sum==divs) sum=0;\n        return sum; \n    }\n\n    int componentValue(vector<int>& nums, vector<vector<int>>& edges) {\n        int sum=0,n=edges.size();\n        for(int i=0; i<nums.size(); i++) sum+=nums[i];\n        vector<int>v[n+1];\n        for(int i=0; i<n; i++)\n        {\n            v[edges[i][0]].push_back(edges[i][1]);\n            v[edges[i][1]].push_back(edges[i][0]);\n        }\n        vector<int>factors;\n        findfactors(factors,sum);\n        int ans=0;\n        for(int i=0; i<factors.size(); i++)\n        {\n            if(dfs(0,-1,v,factors[i],nums)==0) ans=max(ans,sum/factors[i]-1);\n        }\n\n        return ans;\n    }\n};\n```
['C++']