C++ 23 solution
IntuitionFocus on the subtree, how can we determine valid subtrees?
Comparing the subtree sum to a specific valid value was paramount. It was trivial to see tha
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Focus on the subtree, how can we determine valid subtrees?
Comparing the subtree sum to a specific valid value was paramount. It was trivial to see that (number of splits + 1) * component_value = total sum of all the nodes. Using that logic, we have to determine a valid componentValue. So we check that each subtree equals componentValue
# Approach
<!-- Describe your approach to solving the problem. -->
Explained above

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
$$O(ND)$$, D for divisors, n for number of elements in tree.

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(D + )

# Code
```cpp []
class Solution {
public:
   // Function to find all divisors of a number in ascending order
    vector<long long> findDivisors(long long n) {
        vector<long long> divisors;
        for(long long i = 1; i < sqrt(n); ++i){
            if(n % i == 0){
                divisors.push_back(i);
                if(n / i != i){
                    divisors.push_back(n / i);
                }
            }
        }
        sort(divisors.begin(), divisors.end());
        return divisors;
    }

    // Recursive DFS to calculate subtree sums and count valid splits
    long long dfs(int node, int parent, int divisor, const vector<vector<int>> &adj_list, const vector<int> &nums, int &count) {
        long long current_sum = nums[node];
        for(auto &neighbor : adj_list[node]){
            if(neighbor != parent){
                current_sum += dfs(neighbor, node, divisor, adj_list, nums, count);
            }
        }
        if(current_sum == divisor){
            count++;
            return 0; // Reset sum after a valid split
        }
        return current_sum;
    }

    int componentValue(vector<int>& nums, vector<vector<int>>& edges) {
        int n = nums.size();
        // Step 1: Build the adjacency list
        vector<vector<int>> adj_list(n, vector<int>());
        for(auto &edge : edges){
            int a = edge[0];
            int b = edge[1];
            adj_list[a].push_back(b);
            adj_list[b].push_back(a);
        }

        // Step 2: Calculate the total sum
        long long total_sum = 0;
        for(auto &num : nums){
            total_sum += num;
        }

        // Step 3: Find all divisors of the total sum
        vector<long long> divisors = findDivisors(total_sum);
        // Remove the total_sum itself as we cannot split the entire tree
        divisors.pop_back();

        // Step 4: Iterate over divisors in ascending order to maximize splits
        // Alternatively, sort in descending order to find the first valid k quickly
        sort(divisors.begin(), divisors.end(), [&](const long long &a, const long long &b) -> bool {
            return a < b; // Ascending order for maximizing splits
        });

        int max_splits = 0;
        for(auto it = divisors.begin(); it != divisors.end(); ++it){
            long long d = *it;
            // The number of components we need
            int k = total_sum / d;
            int count = 0;
            // Perform DFS to check if we can split the tree into k components with sum d
            dfs(0, -1, d, adj_list, nums, count);
            if(count == k){
                max_splits = max(max_splits, count - 1);
            }
        }

        return max_splits;
    }
};
```
['C++']