Container with Most Water Problem Statement

Given a sequence of 'N' space-separated non-negative integers A[1], A[2], ..., A[i], ..., A[n], where each number in the sequence represents the height of a line drawn at point 'i'. On the Cartesian plane, each line is drawn from coordinate ('i', 0) to coordinate ('i', 'A[i]'). The task is to find two lines such that, together with the x-axis, they form a container that can hold the maximum area of water.

Input:
The first line of input contains an integer 'T' representing the number of test cases.
The next '2*T' lines represent the 'T' test cases.
The first line of each test case contains the number of elements in the sequence.
The second line of each test case contains 'n' space-separated integers which is the given sequence.
Output:
For each test case, return the area of the container that can hold the maximum amount of water using any pair of lines from the given sequence.

Example:

Explanation:

In the provided diagram example, suppose the first red marked line is formed between coordinates (2,0) and (2,10), and the second red marked line is formed between coordinates (5,0) and (5,9). The area of water contained is calculated as (height * width) = (5-2) * 9 = 27, which is the maximum area that can be contained between any two lines on the plane. Therefore, the function should return 3* 9 = 27 in this scenario.

Constraints:

  • 1 <= T <= 50
  • 0 <= N <= 10^4
  • 1 <= A[i] <= 10^5
  • Time Limit: 1 second
Note:
1. You cannot slant the container; the height of the water is equal to the minimum height of the two lines that form the container.
2. Do not print anything; simply return the maximum water area the container can hold.
AnswerBot
4mo

Find two lines to form a container with maximum water capacity based on given heights.

  • Iterate through the array and use two pointers to find the maximum area.

  • Calculate the area using the formula: min(...read more

Help your peers!
Select
Add answer anonymously...

Paytm Software Developer interview questions & answers

A Software Developer was asked 4mo agoQ. Given a sorted array of integers nums and an integer target, write a function to...read more
A Software Developer was asked 6mo agoQ. What is multithreading?
A Software Developer was asked 7mo agoQ. Given a 2D grid map of '1's (land) and '0's (water), count the number of islands...read more

Popular interview questions of Software Developer

A Software Developer was asked 4mo agoQ1. Given a sorted array of integers nums and an integer target, write a function to...read more
A Software Developer was asked 6mo agoQ2. What is multithreading?
A Software Developer was asked 7mo agoQ3. Given a 2D grid map of '1's (land) and '0's (water), count the number of islands...read more
Paytm Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits