Maximum Width of an Equal-Height Fence

Quick Overview

Maximize an equal-height fence’s width using unchanged planks or disjoint pairs of original planks, with each resulting plank contributing one unit of width.

Maximum Width of an Equal-Height Fence

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

# Maximum Width of an Equal-Height Fence You are given the heights of wooden planks in `planks`. Each original plank has width `1`. Build a fence whose resulting planks all have the same height. A resulting plank may be either: - One original plank used unchanged; or - A single plank formed by joining exactly two different original planks, with height equal to the sum of their heights and width `1`. Each original plank can be used at most once, and unused planks may be discarded. Return the maximum possible fence width. ### Function `int maximumWidth(vector<int>& planks)` ### Example 1 **Input:** `planks = [2, 2, 3, 3, 5, 5]` **Output:** `4` **Explanation:** Use the two original planks of height `5`, and form two more height-`5` planks by pairing a `2` with a `3` each time. The four resulting planks have total width `4`. ### Example 2 **Input:** `planks = [4, 4, 4, 8]` **Output:** `3` **Explanation:** Use the three original height-`4` planks and discard the `8`. At height `8`, the original `8` plus one pair of `4`s would give only width `2`. ### Constraints - `1 <= planks.length <= 1000` - `1 <= planks[i] <= 1000000000` ### Clarifications - Different original planks means different indices; equal heights may be paired. - Joining two planks creates one unit of fence width, not two. - A joined plank cannot be joined again: each resulting plank uses exactly one or two originals. - You may choose any common height and do not need to use every original plank. ### What a Strong Answer Covers - Maximizing the number of equal-height resulting planks without reusing originals. - Combining unchanged planks and valid pairs, including pairs of equal heights. - Supporting large heights without scanning every possible integer height.

Overview: Maximize an equal-height fence’s width using unchanged planks or disjoint pairs of original planks, with each resulting plank contributing one unit of width.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Maximum Width of an Equal-Height Fence

You are given the heights of wooden planks in planks. Each original plank has width 1.

Build a fence whose resulting planks all have the same height. A resulting plank may be either:

  • One original plank used unchanged; or
  • A single plank formed by joining exactly two different original planks, with height equal to the sum of their heights and width 1 .

Each original plank can be used at most once, and unused planks may be discarded. Return the maximum possible fence width.

Function

int maximumWidth(vector<int>& planks)

Example 1

Input: planks = [2, 2, 3, 3, 5, 5]

Output: 4

Explanation: Use the two original planks of height 5, and form two more height-5 planks by pairing a 2 with a 3 each time. The four resulting planks have total width 4.

Example 2

Input: planks = [4, 4, 4, 8]

Output: 3

Explanation: Use the three original height-4 planks and discard the 8. At height 8, the original 8 plus one pair of 4s would give only width 2.

Constraints

  • 1 <= planks.length <= 1000
  • 1 <= planks[i] <= 1000000000

Clarifications

  • Different original planks means different indices; equal heights may be paired.
  • Joining two planks creates one unit of fence width, not two.
  • A joined plank cannot be joined again: each resulting plank uses exactly one or two originals.
  • You may choose any common height and do not need to use every original plank.

What a Strong Answer Covers Guidance

  • Maximizing the number of equal-height resulting planks without reusing originals.
  • Combining unchanged planks and valid pairs, including pairs of equal heights.
  • Supporting large heights without scanning every possible integer height.