Javatpoint Logo
Javatpoint Logo

Largest Rectangular Area in Histogram using Segment Tree in C++

Histograms are an essential data structure in computer science with many uses, including data analysis and picture processing. Determining the greatest rectangular area within a histogram is a frequently encountered challenge. In this post, we'll examine a quick and effective method for handling this issue using a C++ Segment Tree data structure.

Understanding the Problem:

Consider a bar chart that shows the frequency or distribution of data, called a histogram. Here, our goal is to determine the greatest rectangle region that can be constructed inside it. In the histogram, every bar has a specific height, and the adjacent bars that have heights greater than or equal to the current bar's height define the width of the rectangle area.

1. Naive Approach:

Seeing each bar in the histogram as a possible location for the beginning of a rectangle area is a simple way to tackle this challenge. We would spread out the space to the left and right of each bar until we came across one that was lower in height. At every stage, we compute the area and record the largest area that is discovered.

  • For big histograms, this strategy is inefficient due to its O(n^2) time complexity, where 'n' is the number of bars in the histogram.
  • We can utilize a Segment Tree (a flexible data structure frequently used in range query problems) to effectively discover the greatest rectangular area in a histogram. It is how the effective method operates:

Efficient Approach Using Segment Tree:

  • First, create a segmentation tree based on the heights of the histogram.
  • Next, go from left to right throughout the histogram, and find the first bar on the left and right with a lower height for each bar.
  • After that, determine the minimal height and breadth of the rectangular area that can be constructed with the current bar based on the places identified in Step 2.
  • Record the largest area discovered throughout the traversal.


Let's take an example to determine the largest rectangular area in histogram using segment tree in C++.


Largest Rectangular Area in Histogram using Segment Tree in C++

Code Explanation:

  1. Iteration over Histogram:
    In this example, we cycle through each bar in the histogram from left to right.
  2. Bar Indices Stack:
    We keep the bar indices in ascending order of height in a stack, which can be accomplished with a vector or other comparable data structure.
  3. Keeping the Stack Together:
    We decide when to pop bar indices off the stack and when to push them on when iterating across the histogram.
  4. Stack Bar Index onto Stack:
    We put the current bar's index onto the stack when we come across a bar whose height is greater than or equal to the bar at the top of the stack?or if the stack is empty.
    By doing this, the stack is guaranteed to retain the indices of bars that could help create larger rectangles.
  5. Bars of Pop From Stack:
    We begin popping bars from the stack whenever we come across a bar whose height is less than the bar at the top of the stack.
    We determine the area that can be formed by each popped bar by measuring its height and width (the difference between the present position and the top of the stack).
  6. Max Area Updating:
    If the calculated area is more than the current maximum, we update the maximum area as we calculate the area for each popping bar.
  7. Iterate the Procedure:
    Until we either reach the end of the histogram or come across a bar that is lower in height than the bar at the top of the stack, we keep pushing and popping bars.
  8. Bars Still in the Stack:
    There might be some bars in the stack left behind after processing every bar in the histogram.
    We keep popping bars and figuring out areas until the stack is empty.
  9. Maximum Area:
    The largest rectangular area that can be formed in the histogram is the maximum area that is encountered during this operation.

Next TopicC++ async await

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA