Java🔥Easy to Understand
\n\n# Code\n\nclass Solution {\n    int components=0;\n    public int getans(ArrayList<Integer>[] graph,int src,int par,int[]nums,int k){\n        int sum =0;\n
\n\n# Code\n```\nclass Solution {\n    int components=0;\n    public int getans(ArrayList<Integer>[] graph,int src,int par,int[]nums,int k){\n        int sum =0;\n        for(int nbr : graph[src]){\n            if(nbr!=par){\n                sum+=getans(graph,nbr,src,nums,k);\n            }\n        }\n        sum+=nums[src];\n        if(sum==k) components++;\n        return sum==k?0:sum;\n    }\n    public int componentValue(int[] nums, int[][] edges) {\n        ArrayList<Integer>[] graph = new ArrayList[nums.length];\n        for(int i=0;i<nums.length;i++) graph[i]=new ArrayList<>();\n        for(int [] e : edges){\n            int u = e[0];\n            int v = e[1];\n            graph[u].add(v);\n            graph[v].add(u);\n        }\n        int sum=0;\n        int maxm=0;\n        for(int ele:nums){\n            sum+=ele;\n            maxm=Math.max(maxm,ele);\n        }\n        int maxComponents = 0;\n        for(int i=maxm;i<=sum;i++){\n            components=0;\n            if(sum%i==0){\n                int x = getans(graph,0,-1,nums,i);\n                if(x==0){\n                    // System.out.println(i + " " + components);\n                    maxComponents=Math.max(maxComponents,components);\n                }\n            }\n        }\n        return maxComponents-1;\n    }\n}\n```
['Java']