Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7984785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:22:56+00:00 2026-06-04T11:22:56+00:00

Given n non-negative integers a1, a2, …, an, where each represents a point at

  • 0

Given n non-negative integers a1, a2, …, an, where each represents a
point at coordinate (i, ai). n vertical lines are drawn such that the
two endpoints of line i is at (i, ai) and (i, 0). Find two lines,
which together with x-axis forms a container, such that the container
contains the most water.

Note: You may not slant the container.

One solution could be that we take each and every line and find area with every line. This takes O(n^2). Not time efficient.

Another solution could be using DP to find the maximum area for every index, and then at index n, we will get the maximum area.
I think it’s O(n).

Could there be more better solutions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:22:58+00:00Added an answer on June 4, 2026 at 11:22 am

    Many people here are mistaking this problem to maximal rectangle problem, which is not the case.

    Solution

    1. Delete all the elements aj such that ai >= aj =< ak and i > j < k. This can be done in linear time.
      1. Find the maximum value am
      2. Let as = a1
      3. For j = 2 through m-1, if as >= aj, delete aj, else as = aj
      4. Let as = an
      5. For j = n-1 through m+1, if as >= aj, delete aj, else as = aj
    2. Notice that the resulting values look like a pyramid, that is, all the elements on the left of the maximum are strictly increasing and on the right are strictly decreasing.
    3. i=1, j=n. m is location of max.
    4. While i<=m and j>=m
      1. Find area between ai and aj and keep track of the max
      2. If ai < aj, i+=1, else j-=1

    Complexity is linear (O(n))

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a non-negative integer n and an arbitrary set of inequalities that are user-defined
Given two non-related classes A and B, how to call A.method with an object
Given the following recursive function: // Pre-condition: y is non-negative. int mysterious(int x, int
Im stuck with the below problem. Problem Statement: Given a non-negative int n, return
Given a list of integers, how can I best find an integer that is
I have 'n' number of amounts (non-negative integers). My requirement is to determine an
Given the following indented text: two spaces four six non-leading spaces I'd like to
Given: I added a non-nullable foreign key to a table. I settled on a
Given a crash report (non-symbolicated) on iOS, is there a way to determine which
Given an arbitrary file descriptor, can I make it blocking if it is non-blocking?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.