algoblazerEarly Access
LearnProblemsStudy GroupsMock InterviewTrail BlazeLeaderboard
Log inSign up

All problems

‹ Back to map
1 shown · 0/347 solved

Count Labeled Trees Balanced by Side

PlatinumCommunity Beta
Asked atTower Research Capital
Solve problem →
2000ms256MBAdded Oct 26, 2026

You are given an integer array workload of length n. There are n distinct nodes, one per index, and the node at index i starts with workload[i] units. You can assume the values in workload add up to n.

Consider building a tree on all n nodes. A tree is a set of n - 1 edges that connects every node and contains no cycle. Two trees are different if some pair of nodes is joined by an edge in one of them and not in the other.

Once a tree is built, consider an operation where you pick two nodes that are exactly two edges apart in it and move one unit from the first to the second. The first node must hold at least one unit. You can perform this operation any number of times.

A tree is balanced if some sequence of operations leaves every node holding exactly one unit.

Return the number of balanced trees. Mod the result by 10 ** 9 + 7.

Examples

Example 1

Input: workload = [1,1,1]
Output: 3
Explanation: Every tree on 3 nodes is a path, and there are 3 of them, one per choice of middle node. Every node already holds one unit, so no operation is needed and all 3 trees are balanced.
Read full statement →

Constraints

  • 1≤n≤3001 \leq n \leq 3001≤n≤300 where n=n =n= workload.length
  • 0≤0 \leq0≤ workload[i] ≤n\leq n≤n
  • workload[0] +++ workload[1] +…++ \ldots ++…+ workload[n-1] =n= n=n

Details

Solved by0 people
Time limit2000ms
Memory256MB
AddedOct 26, 20263 hours ago