Java: Optimal solution
Code\n\nclass Solution {\n    public int dfs(int src,ArrayList<ArrayList<Integer>> al,int[] nums,int sum,boolean[] vis){\n        vis[src]=true;\n        int cu
# Code\n```\nclass Solution {\n    public int dfs(int src,ArrayList<ArrayList<Integer>> al,int[] nums,int sum,boolean[] vis){\n        vis[src]=true;\n        int curr = nums[src];\n        for(Integer z : al.get(src)){\n            if(!vis[z]){\n                int s = dfs(z,al,nums,sum,vis);\n                // if sum is not possible in some subtree\n                // it\'s a fail case we return from here\n                if(s==-1)\n                    return s;\n                curr+=s;\n            }\n        }\n        // if curr value is greater than sum\n        // then we cannot create sum anymore, so return -1\n        if(curr>sum)\n            return -1;\n        // is possible, we will do a split of component and\n        // return 0 as sum of the subtree\n        if(curr==sum)\n            return 0;\n        return curr;\n    }\n \n    public int componentValue(int[] nums, int[][] edges) {\n        int sum=0;\n        ArrayList<ArrayList<Integer>> al=new ArrayList<>();\n        for(int x : nums){\n            sum+=x;\n            al.add(new ArrayList<>());\n        }\n        for(int[] x : edges){\n            al.get(x[0]).add(x[1]);\n            al.get(x[1]).add(x[0]);\n        }\n        int ans=0;\n        int sq=(int)Math.sqrt(sum);\n        // Iterating over each divisor of the total sum\n        // as we want a equal split of sum among all components\n        for(int i=1;i<=sq;i++){\n            if(sum%i==0){\n                int z=sum/i;\n                boolean vis[]=new boolean[nums.length];\n                // Try to make sum as z, if possible\n                // then we need to remove i-1 edges only\n                // E.g : i=3, sum=18 : then you need sum as 18/3=6\n                // for 3 components and 3 components could be made\n                // by removing 2(i-1) edges.\n                int op=dfs(0,al,nums,z,vis);\n                if(op==0){\n                    // Taking max of all the edge values\n                    ans=Integer.max(ans,i-1);\n                }\n                if(sum%z==0){\n                    Arrays.fill(vis,false);\n                    // Similarly trying for other values\n                    op=dfs(0,al,nums,i,vis);\n                    if(op==0){\n                        // Taking max of all the edge values\n                        ans=Integer.max(ans,z-1);\n                    }\n                }\n            }\n        }\n        return ans;\n    }\n}\n```
['Tree', 'Java']