Largest Rectangle in Histogram Problem Statement

You are given an array/list HEIGHTS of length N, where each element represents the height of a histogram bar. The width of each bar is considered to be 1.

Your task is to compute the area of the largest rectangle that can be formed within the bounds of the given histogram.

Example:

Input:
{2, 1, 5, 6, 2, 3}
Output:
10
Explanation:

In the given histogram, the largest rectangle possible has an area of 10.

Input:

The first line contains a single integer 'T’, representing the number of test cases. For each test case, the first line contains a single integer 'N' representing the number of elements in the array/list. The second line contains 'N' single space-separated integers, denoting the elements of the array/list HEIGHTS.

Output:

For each test case, output an integer that represents the area of the largest rectangle possible in the given histogram.

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 10^6
  • 0 ≤ HEIGHTS[i] ≤ 10^9
  • Time Limit: 1 sec.
Note:

You do not need to print anything; it has already been taken care of. Just implement the given function.

AnswerBot
1y

The task is to find the largest rectangle possible in a given histogram and return its area.

  • Iterate through the histogram and maintain a stack to keep track of the indices of the bars in non-decreasin...read more

Help your peers!
Add answer anonymously...
Texas Instruments Software Developer Intern 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