Factorization || Brute Force || C++
\nclass Solution {\npublic:\n    vector<vector<int>> graf;\n    vector<int> nums;\n    \n    int comp, val;\n    int dfs(int v, int par) {\n        int tot = nu
```\nclass Solution {\npublic:\n    vector<vector<int>> graf;\n    vector<int> nums;\n    \n    int comp, val;\n    int dfs(int v, int par) {\n        int tot = nums[v];\n        for(auto x: graf[v]) {\n            if(x == par) continue;\n            tot += dfs(x, v);\n        }\n        \n        if(tot == val) {\n            //if a component of sum = val is found decrease number of component\'s by 1\n            //and return 0 because the component is detatched from this node\n            comp--;\n            return 0;\n        }\n        //this node cannot form a component with sum = val so maybe the parent would be able to make it\n        return tot;\n    }\n    int componentValue(vector<int>& nums, vector<vector<int>>& edges) {\n        int n = nums.size();\n        \n        graf.resize(n);\n        this->nums = nums;\n        \n        for(auto x: edges) {\n            graf[x[0]].push_back(x[1]);\n            graf[x[1]].push_back(x[0]);\n        }\n        \n        //if all values are equal then n - 1 edges can be deleted\n        if(count(nums.begin(), nums.end(), nums[0]) == n) return n - 1;\n        \n        int sum = accumulate(nums.begin(), nums.end(), 0);\n        int res = 0;\n        \n        //now the sum of every component has to be same so we can factorize the total sum\n        //and check for every possible values\n        //For ex: sum = 18\n        //18 = 3 * 6\n        //check if the graph can be broken on some edges such that there are 3 components with sum 6\n        //do the same operation in the flip order i.e check if the graph can be broken into 6 components with sum 3\n        //now number of edges deleted = number of components - 1\n\t\t//Time Complexity: O(n * sqrt(sum))\n        for(int i = 2; i * i <= sum; ++i) {\n            if(sum % i == 0) {\n                comp = i, val = sum / i;\n                dfs(0, -1);\n                if(comp == 0) res = max(res, i - 1);\n                \n                comp = sum / i, val = i;\n                dfs(0, -1);\n                if(comp == 0) res = max(res, (sum / i) - 1);\n            }\n        }\n        \n        return res;\n    }\n};\n```
['C', 'C++']