Algorithmic patterns · 11 / 15 Exercise Hard

Container with most water

Objective

Each number is the height of a wall. Two walls form a container of area distance × smaller height. With two pointers from the ends, always moving the one on the lower wall finds the maximum in O(n).

Instructions

  • Read a line with the heights (non-negative integers, at least two).
  • Show Maximum area: x with the largest possible area.
Input
1 8 6 2 5 4 8 3 7
Output
Maximum area: 49

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise