Calculate Weight in a Nested Balanced Mobile System
Math & Bit ManipulationAuto-graded · 15 test casesLast updated
Tests systematic application of balance constraints in a multi-level mobile where equal-arm bars divide weight evenly.
Calculate Weight in a Nested Balanced Mobile System
You are analyzing a hanging mobile where all shapes together have a known total weight. Each horizontal bar in the mobile has zero mass and features two arms of equal length. When arms are equal, balance requires that the total weight hanging from the left arm equals the total weight hanging from the right arm.
The mobile is structured as follows from top to bottom:
Top bar (Bar 1):
- Left side: A complete sub-mobile (described below)
- Right side: Other shapes (not detailed, but contribute to total balance)
The left sub-mobile has its own bar (Bar 2):
- Left side: one orange octagon + one pink pentagon (hanging together)
- Right side: Bar 3 (described below)
Bar 3:
- Left side: Bar 4 (described below)
- Right side: one gray octagon + one pink pentagon (hanging together)
Bar 4:
- Left side: two purple hexagons (their combined weight hangs from this side)
- Right side: one green triangle
Important details:
- Every bar is massless and has equal-length arms on both sides
- Balance means: left side total weight = right side total weight
- The given
total_weightis the sum of all shape weights across the entire mobile (bars contribute nothing) - When multiple shapes hang from the same side of a bar, their weights add together for that side's total
Determine the weight of the one green triangle.
Return the result as an integer representing pounds.
Constraints
total_weightis a positive integer- All bars have equal-length arms and zero mass
- The mobile is in complete balance
- The result is always a whole number
Examples
Example 1
Input: calculate_triangle_weight(160)
Output: 10
Hints
Hint 1
A bar with equal arms in balance means each side supports exactly half of the bar's total hanging weight.
Hint 2
Trace the path from the top bar down to the green triangle, applying the balance rule at each level.
Hint 3
The green triangle appears after following the left branch through multiple nested bars.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The solution relies on the principle that a balanced bar with equal-length arms must have equal weight on each side. This means each side carries exactly half of the total weight supported by that bar.
We trace the path from the top bar down to the green triangle, applying this halving rule at each bar:
-
Bar 1 (top): Supports the entire
total_weight. Left side and right side each supporttotal_weight / 2. -
Bar 2: Located on the left side of Bar 1, so it receives
total_weight / 2. Its left and right sides each supporttotal_weight / 4. -
Bar 3: Located on the right side of Bar 2, so it receives
total_weight / 4. Its left and right sides each supporttotal_weight / 8. -
Bar 4: Located on the left side of Bar 3, so it receives
total_weight / 8. Its left and right sides each supporttotal_weight / 16. -
Green triangle: Hangs from the right side of Bar 4, so its weight is
total_weight / 16.
The green triangle's weight equals the total mobile weight divided by 16 (or halved four times).
Why This Works
Equal-arm balance creates a simple weight relationship. For a bar with arm length L on both sides, rotational equilibrium requires weight_left × L = weight_right × L, which reduces to weight_left = weight_right. Each side therefore holds half the bar's total load.
Following the nested structure through four bars, we apply this halving operation four times: total_weight / 2 / 2 / 2 / 2 = total_weight / 16.
Complexity
- Time complexity: O(1) — single division operation
- Space complexity: O(1) — constant memory usage
Solution
def calculate_triangle_weight(total_weight: int) -> int:
# With equal arms, each bar splits its load 50-50 between sides.
# Tracing the path to the green triangle:
# Bar 1: total_weight -> each side gets total_weight / 2
# Bar 2 (on left of Bar 1): gets total_weight / 2 -> each side gets total_weight / 4
# Bar 3 (on right of Bar 2): gets total_weight / 4 -> each side gets total_weight / 8
# Bar 4 (on left of Bar 3): gets total_weight / 8 -> each side gets total_weight / 16
# Green triangle (on right of Bar 4): weighs total_weight / 16
return total_weight // 16