Tallest Billboard - Problem

Imagine you're constructing a massive billboard that needs to be perfectly balanced with two steel supports of equal height on each side. You have a collection of steel rods with various lengths that can be welded together to create these supports.

Your goal is to maximize the height of your billboard by cleverly selecting and grouping rods to form two supports of identical height. Each rod can be used in at most one support, or not used at all.

For example, with rods of lengths [1, 2, 3, 6], you could create two supports of height 6: one using the single rod of length 6, and another by welding together rods of lengths 1, 2, and 3.

Return the maximum possible height of equal supports. If it's impossible to create two equal-height supports, return 0.

Input & Output

example_1.py โ€” Basic Case
$ Input: rods = [1, 2, 3, 6]
โ€บ Output: 6
๐Ÿ’ก Note: We can build two supports of height 6. Use rod of length 6 for one support, and combine rods [1,2,3] for the other support (1+2+3=6).
example_2.py โ€” No Solution
$ Input: rods = [1, 2, 3, 4, 5, 6]
โ€บ Output: 10
๐Ÿ’ก Note: We can build two supports of height 10. One support uses rods [2,3,5] (2+3+5=10), another uses rods [4,6] (4+6=10). Rod [1] is unused.
example_3.py โ€” Impossible Case
$ Input: rods = [1, 2]
โ€บ Output: 0
๐Ÿ’ก Note: It's impossible to create two equal-height supports. We can't split rods, and no combination gives us equal heights.

Visualization

Tap to expand
๐Ÿ—๏ธ Billboard Construction ProcessLeft SupportRight SupportRod: 3Rod: 1Rod: 2Rod: 6Balance the heights!Height: 6Both supports achieve equal height of 6 units
Understanding the Visualization
1
Start with empty supports
Both left and right supports begin at height 0
2
For each rod, decide placement
Add to left support, right support, or don't use it
3
Track all possible differences
Use DP to efficiently explore all valid combinations
4
Find maximum equal height
Return the highest achievable height when difference = 0
Key Takeaway
๐ŸŽฏ Key Insight: Track the difference between support heights rather than absolute heights - this dramatically reduces the state space and enables efficient dynamic programming!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(3^n)

For each of n rods, we have 3 choices (left, right, unused), leading to 3^n combinations

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Recursion depth is at most n levels deep

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค rods.length โ‰ค 20
  • 1 โ‰ค rods[i] โ‰ค 1000
  • Sum of all rod lengths โ‰ค 5000
  • Each rod can be used in at most one support or not used at all
Asked in
Google 15 Facebook 12 Amazon 8 Microsoft 6
26.8K Views
Medium Frequency
~35 min Avg. Time
892 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