Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

27. Container With Most Water

MediumDay 8 of 60Two Pointers

You are given an array height where height[i] is the height of a vertical line drawn at position i. Pick two lines so that, together with the x-axis, they hold the most water. The water level is limited by the shorter line, and the width is the distance between the two positions. Return that maximum amount of water.

Example 1
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
The lines at index 1 (height 8) and index 8 (height 7) are 7 apart, so the area is min(8, 7) * 7 = 49.
Example 2
Input: height = [1,1]
Output: 1
Constraints
  • 2 <= height.length <= 10^5
  • 0 <= height[i] <= 10^4
Hints
Frequently asked at: Amazon, Google, Meta, Goldman Sachs, Adobe
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min