Tallest Billboard - Problem

You are installing a billboard and want it to have the largest height. The billboard will have two steel supports, one on each side. Each steel support must be an equal height.

You are given a collection of rods that can be welded together. For example, if you have rods of lengths 1, 2, and 3, you can weld them together to make a support of length 6.

Return the largest possible height of your billboard installation. If you cannot support the billboard, return 0.

Input & Output

Example 1 — Basic Case
$ Input: rods = [1,2,3,6]
Output: 6
💡 Note: We can build supports of equal height 6: Left support uses rods [1,2,3] with total length 6, Right support uses rod [6] with length 6
Example 2 — No Solution
$ Input: rods = [1,2,3,4]
Output: 0
💡 Note: No way to split rods [1,2,3,4] into two equal-height supports. Total sum is 10, so we need each support to have height 5, but no subset sums to 5
Example 3 — Multiple Solutions
$ Input: rods = [1,2]
Output: 0
💡 Note: Only one rod per support would give heights 1 and 2 (not equal). Cannot make equal heights with these rods

Constraints

  • 1 ≤ rods.length ≤ 20
  • 1 ≤ rods[i] ≤ 1000

Visualization

Tap to expand
Tallest Billboard Problem INPUT rods = [1, 2, 3, 6] Rods available: 1 2 3 6 Goal: Two equal height supports for maximum billboard height Input: rods = [1, 2, 3, 6] ALGORITHM STEPS 1 DP with Difference Track diff between supports 2 State: dp[diff] = max_sum For each diff, store taller sum 3 Process Each Rod Add to left, right, or skip 4 Find dp[0] Diff=0 means equal supports DP Transitions: dp[diff+rod] = max(dp[diff+rod], dp[diff]+rod) dp[diff-rod] = max(dp[diff-rod], dp[diff]) Time: O(n * sum) | Space: O(sum) FINAL RESULT Optimal Billboard Configuration: 1+2+3=6 BILLBOARD 6 Height = 6 Left support: 1 + 2 + 3 = 6 Right support: 6 Both equal height! Output: 6 Maximum Height OK Key Insight: Instead of tracking two support heights, we track their DIFFERENCE. When diff = 0, supports are equal. For each rod, we have 3 choices: add to taller support, add to shorter support, or skip the rod. This reduces state space from O(sum^2) to O(sum), making the solution efficient. TutorialsPoint - Tallest Billboard | Optimal DP Solution
Asked in
Google 25 Facebook 18 Amazon 12
28.0K Views
Medium Frequency
~45 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen