Largest Rectangular Area In A Histogram
Given an array HEIGHTS
of length N
, where each element represents the height of a histogram bar and the width of each bar is 1, determine the area of the largest rectangle that can be formed within the bounds of the histogram.
Input
The first line contains a single integer 'T' representing the number of test cases.
Each test case consists of:
- The first line contains an integer 'N', the number of elements in 'HEIGHTS'.
- The second line contains 'N' space-separated integers representing the heights of the histogram bars.
Output
Output one integer per test case, the area of the largest rectangle that can be contained within the histogram.
Example
Input:
HEIGHTS = [2, 1, 5, 6, 2, 3]
Output:
10
Constraints
1 <= T <= 10
1 <= N <= 10^6
0 <= HEIGHTS[i] <= 10^9
- Time Limit: 1 sec
Note
The implementation function will handle the output printing. You only need to calculate and return the appropriate values.

AnswerBot
4mo
Find the largest rectangular area in a histogram given the heights of the bars.
Use a stack to keep track of the indices of the bars in non-decreasing order of heights.
Calculate the area of the rectang...read more
Help your peers!
Add answer anonymously...
Snapdeal Software Developer interview questions & answers
A Software Developer was asked Q. Given two linked lists that merge at a point, find the merging point.
A Software Developer was asked Q. Given an M x N matrix, how many rectangles are there?
A Software Developer was asked Q. Find a four-digit number in the form aabb that is a perfect square.
Popular interview questions of Software Developer
A Software Developer was asked Q1. Given two linked lists that merge at a point, find the merging point.
A Software Developer was asked Q2. Given an M x N matrix, how many rectangles are there?
A Software Developer was asked Q3. Find a four-digit number in the form aabb that is a perfect square.
Stay ahead in your career. Get AmbitionBox app


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
AmbitionBox Awards
Get AmbitionBox app

