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!
Select
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.
Snapdeal 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