Asked inDunzo,SDE-2

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.
Be the first one to answer
Add answer anonymously...
Dunzo SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter