Arrays January 18 ,2026

Largest Rectangle in Histogram

Problem Statement

You are given an array heights[] where each element represents the height of a bar in a histogram.
Each bar has a width of 1.

Find the area of the largest rectangle that can be formed within the histogram.

Example 1

Input

heights = [2,1,5,6,2,3]

Output

10

Explanation
The largest rectangle is formed by bars of height 5 and 6 with width 2.
Area = 5 × 2 = 10

Example 2

Input

heights = [2,4]

Output

4

Why This Problem Is Important

  • Classic stack-based interview question
  • Tests monotonic stack understanding
  • Used in matrix rectangle problems
  • Appears frequently in FAANG interviews
  • Foundation for advanced histogram problems

Approaches Overview

ApproachTechniqueTimeSpace
Approach 1Brute ForceO(n²)O(1)
Approach 2Better Brute (Expand Left & Right)O(n²)O(1)
Approach 3Stack (Optimal)O(n)O(n)

Approach 1: Brute Force

Idea

For every bar, try extending to the right and calculate the minimum height.

Algorithm

  1. For each index i
  2. Set minHeight = heights[i]
  3. For every index j ≥ i
    • minHeight = min(minHeight, heights[j])
    • Area = minHeight × (j - i + 1)
  4. Track maximum area

Time & Space Complexity

  • Time: O(n²)
  • Space: O(1)

C

#include 

int main() {
    int h[] = {2,1,5,6,2,3};
    int n = 6, maxArea = 0;

    for(int i = 0; i < n; i++) {
        int minH = h[i];
        for(int j = i; j < n; j++) {
            if(h[j] < minH) minH = h[j];
            int area = minH * (j - i + 1);
            if(area > maxArea) maxArea = area;
        }
    }

    printf("Input: [2,1,5,6,2,3]\n");
    printf("Output: %d\n", maxArea);
    return 0;
}

C++

#include 
using namespace std;

int main() {
    int h[] = {2,1,5,6,2,3};
    int n = 6, maxArea = 0;

    for(int i = 0; i < n; i++) {
        int minH = h[i];
        for(int j = i; j < n; j++) {
            minH = min(minH, h[j]);
            maxArea = max(maxArea, minH * (j - i + 1));
        }
    }

    cout << "Input: [2,1,5,6,2,3]" << endl;
    cout << "Output: " << maxArea << endl;
    return 0;
}

Java

public class HistogramBrute {
    public static void main(String[] args) {
        int[] h = {2,1,5,6,2,3};
        int maxArea = 0;

        for(int i = 0; i < h.length; i++) {
            int minH = h[i];
            for(int j = i; j < h.length; j++) {
                minH = Math.min(minH, h[j]);
                maxArea = Math.max(maxArea, minH * (j - i + 1));
            }
        }

        System.out.println("Input: [2,1,5,6,2,3]");
        System.out.println("Output: " + maxArea);
    }
}

Python

h = [2,1,5,6,2,3]
n = len(h)
maxArea = 0

for i in range(n):
    minH = h[i]
    for j in range(i, n):
        minH = min(minH, h[j])
        maxArea = max(maxArea, minH * (j - i + 1))

print("Input: [2,1,5,6,2,3]")
print("Output:", maxArea)

JavaScript

let h = [2,1,5,6,2,3];
let maxArea = 0;

for (let i = 0; i < h.length; i++) {
    let minH = h[i];
    for (let j = i; j < h.length; j++) {
        minH = Math.min(minH, h[j]);
        maxArea = Math.max(maxArea, minH * (j - i + 1));
    }
}

console.log("Input: [2,1,5,6,2,3]");
console.log("Output:", maxArea);

C#

using System;

class Program {
    static void Main() {
        int[] h = {2,1,5,6,2,3};
        int maxArea = 0;

        for(int i = 0; i < h.Length; i++) {
            int minH = h[i];
            for(int j = i; j < h.Length; j++) {
                minH = Math.Min(minH, h[j]);
                maxArea = Math.Max(maxArea, minH * (j - i + 1));
            }
        }

        Console.WriteLine("Input: [2,1,5,6,2,3]");
        Console.WriteLine("Output: " + maxArea);
    }
}

Final Output 

Input: [2,1,5,6,2,3]
Output: 10

Approach 2: Better Brute (Expand Left & Right)

Idea

For each bar, expand left and right while height ≥ current bar.

Algorithm

  1. For each index i
  2. Move left while height ≥ h[i]
  3. Move right while height ≥ h[i]
  4. Width = right - left - 1
  5. Area = h[i] × width

Time & Space Complexity

  • Time: O(n²)
  • Space: O(1)

C

#include 

int main() {
    int h[] = {2,1,5,6,2,3};
    int n = 6, maxArea = 0;

    for(int i = 0; i < n; i++) {
        int left = i, right = i;

        while(left > 0 && h[left - 1] >= h[i]) left--;
        while(right < n - 1 && h[right + 1] >= h[i]) right++;

        int width = right - left + 1;
        int area = h[i] * width;
        if(area > maxArea) maxArea = area;
    }

    printf("Input: [2,1,5,6,2,3]\n");
    printf("Output: %d\n", maxArea);
    return 0;
}

C++

#include 
using namespace std;

int main() {
    int h[] = {2,1,5,6,2,3};
    int n = 6, maxArea = 0;

    for(int i = 0; i < n; i++) {
        int left = i, right = i;

        while(left > 0 && h[left - 1] >= h[i]) left--;
        while(right < n - 1 && h[right + 1] >= h[i]) right++;

        int width = right - left + 1;
        maxArea = max(maxArea, h[i] * width);
    }

    cout << "Input: [2,1,5,6,2,3]" << endl;
    cout << "Output: " << maxArea << endl;
    return 0;
}

Java

public class HistogramBetterBrute {
    public static void main(String[] args) {
        int[] h = {2,1,5,6,2,3};
        int maxArea = 0;

        for(int i = 0; i < h.length; i++) {
            int left = i, right = i;

            while(left > 0 && h[left - 1] >= h[i]) left--;
            while(right < h.length - 1 && h[right + 1] >= h[i]) right++;

            int width = right - left + 1;
            maxArea = Math.max(maxArea, h[i] * width);
        }

        System.out.println("Input: [2,1,5,6,2,3]");
        System.out.println("Output: " + maxArea);
    }
}

Python

h = [2,1,5,6,2,3]
n = len(h)
maxArea = 0

for i in range(n):
    left = i
    right = i

    while left > 0 and h[left - 1] >= h[i]:
        left -= 1
    while right < n - 1 and h[right + 1] >= h[i]:
        right += 1

    width = right - left + 1
    maxArea = max(maxArea, h[i] * width)

print("Input: [2,1,5,6,2,3]")
print("Output:", maxArea)

JavaScript

let h = [2,1,5,6,2,3];
let maxArea = 0;

for (let i = 0; i < h.length; i++) {
    let left = i, right = i;

    while (left > 0 && h[left - 1] >= h[i]) left--;
    while (right < h.length - 1 && h[right + 1] >= h[i]) right++;

    let width = right - left + 1;
    maxArea = Math.max(maxArea, h[i] * width);
}

console.log("Input: [2,1,5,6,2,3]");
console.log("Output:", maxArea);

C#

using System;

class Program {
    static void Main() {
        int[] h = {2,1,5,6,2,3};
        int maxArea = 0;

        for(int i = 0; i < h.Length; i++) {
            int left = i, right = i;

            while(left > 0 && h[left - 1] >= h[i]) left--;
            while(right < h.Length - 1 && h[right + 1] >= h[i]) right++;

            int width = right - left + 1;
            maxArea = Math.Max(maxArea, h[i] * width);
        }

        Console.WriteLine("Input: [2,1,5,6,2,3]");
        Console.WriteLine("Output: " + maxArea);
    }
}

Final Output

Input: [2,1,5,6,2,3]
Output: 10

Approach 3: Stack (Optimal)

Idea

Use a monotonic increasing stack.
When a smaller bar appears, calculate area of popped bars.

Algorithm

  1. Use stack to store indices
  2. Traverse bars
  3. While stack not empty and current bar < stack top:
    • Pop height
    • Width = current index - stack top - 1
  4. Push index
  5. Process remaining stack

Time & Space Complexity

  • Time: O(n)
  • Space: O(n)

C

#include 

int main() {
    int h[] = {2,1,5,6,2,3};
    int n = 6;
    int stack[100], top = -1;
    int maxArea = 0;

    for(int i = 0; i <= n; i++) {
        int currHeight = (i == n) ? 0 : h[i];

        while(top != -1 && h[stack[top]] > currHeight) {
            int height = h[stack[top--]];
            int width = (top == -1) ? i : i - stack[top] - 1;
            int area = height * width;
            if(area > maxArea) maxArea = area;
        }
        stack[++top] = i;
    }

    printf("Input: [2,1,5,6,2,3]\n");
    printf("Output: %d\n", maxArea);
    return 0;
}

C++

#include 
#include 
using namespace std;

int main() {
    int h[] = {2,1,5,6,2,3};
    int n = 6;
    stack st;
    int maxArea = 0;

    for(int i = 0; i <= n; i++) {
        int currHeight = (i == n) ? 0 : h[i];

        while(!st.empty() && h[st.top()] > currHeight) {
            int height = h[st.top()];
            st.pop();
            int width = st.empty() ? i : i - st.top() - 1;
            maxArea = max(maxArea, height * width);
        }
        st.push(i);
    }

    cout << "Input: [2,1,5,6,2,3]" << endl;
    cout << "Output: " << maxArea << endl;
    return 0;
}

Java

import java.util.Stack;

public class HistogramOptimal {
    public static void main(String[] args) {
        int[] h = {2,1,5,6,2,3};
        Stack st = new Stack<>();
        int maxArea = 0;

        for(int i = 0; i <= h.length; i++) {
            int currHeight = (i == h.length) ? 0 : h[i];

            while(!st.isEmpty() && h[st.peek()] > currHeight) {
                int height = h[st.pop()];
                int width = st.isEmpty() ? i : i - st.peek() - 1;
                maxArea = Math.max(maxArea, height * width);
            }
            st.push(i);
        }

        System.out.println("Input: [2,1,5,6,2,3]");
        System.out.println("Output: " + maxArea);
    }
}

Python

h = [2,1,5,6,2,3]
stack = []
maxArea = 0
n = len(h)

for i in range(n + 1):
    currHeight = 0 if i == n else h[i]

    while stack and h[stack[-1]] > currHeight:
        height = h[stack.pop()]
        width = i if not stack else i - stack[-1] - 1
        maxArea = max(maxArea, height * width)

    stack.append(i)

print("Input: [2,1,5,6,2,3]")
print("Output:", maxArea)

JavaScript

let h = [2,1,5,6,2,3];
let stack = [];
let maxArea = 0;
let n = h.length;

for (let i = 0; i <= n; i++) {
    let currHeight = (i === n) ? 0 : h[i];

    while (stack.length && h[stack[stack.length - 1]] > currHeight) {
        let height = h[stack.pop()];
        let width = stack.length === 0 ? i : i - stack[stack.length - 1] - 1;
        maxArea = Math.max(maxArea, height * width);
    }
    stack.push(i);
}

console.log("Input: [2,1,5,6,2,3]");
console.log("Output:", maxArea);

C#

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        int[] h = {2,1,5,6,2,3};
        Stack st = new Stack();
        int maxArea = 0;

        for(int i = 0; i <= h.Length; i++) {
            int currHeight = (i == h.Length) ? 0 : h[i];

            while(st.Count > 0 && h[st.Peek()] > currHeight) {
                int height = h[st.Pop()];
                int width = st.Count == 0 ? i : i - st.Peek() - 1;
                maxArea = Math.Max(maxArea, height * width);
            }
            st.Push(i);
        }

        Console.WriteLine("Input: [2,1,5,6,2,3]");
        Console.WriteLine("Output: " + maxArea);
    }
}

Final Output

Input: [2,1,5,6,2,3]
Output: 10

Summary

  • Brute force is intuitive but slow
  • Expand left-right improves clarity but still O(n²)
  • Stack approach is optimal and interview-preferred
  • Monotonic stack is the key concept

Next Problem in the Series

Maximum Sum Rectangle in 2D Matrix
 

Sanjiv
0

You must logged in to post comments.

Related Blogs

Find the S...
Arrays February 02 ,2026

Find the Second Smal...

Find the S...
Arrays January 01 ,2026

Find the Sum of All...

Find the M...
Arrays January 01 ,2026

Find the Maximum Ele...

Find the M...
Arrays January 01 ,2026

Find the Minimum Ele...

Count Even...
Arrays January 01 ,2026

Count Even and Odd N...

Search an...
Arrays January 01 ,2026

Search an Element in...

Copy One A...
Arrays January 01 ,2026

Copy One Array into...

Reverse an...
Arrays January 01 ,2026

Reverse an Array

Print Alte...
Arrays January 01 ,2026

Print Alternate Elem...

Find the L...
Arrays January 01 ,2026

Find the Length of a...

Check if a...
Arrays January 01 ,2026

Check if an Array is...

Find the F...
Arrays January 01 ,2026

Find the First Eleme...

Find the L...
Arrays January 01 ,2026

Find the Last Elemen...

Count the...
Arrays January 01 ,2026

Count the Number of...

Replace Al...
Arrays January 01 ,2026

Replace All Elements...

Sum of Ele...
Arrays January 01 ,2026

Sum of Elements at E...

Sum of Ele...
Arrays January 01 ,2026

Sum of Elements at O...

Find the A...
Arrays January 01 ,2026

Find the Average of...

Count the...
Arrays January 01 ,2026

Count the Number of...

Remove Dup...
Arrays January 01 ,2026

Remove Duplicate Ele...

Move All Z...
Arrays January 01 ,2026

Move All Zeros to th...

Rotate an...
Arrays January 01 ,2026

Rotate an Array by K...

Rotate an...
Arrays January 01 ,2026

Rotate an Array by O...

Check if T...
Arrays January 01 ,2026

Check if Two Arrays...

Merge Two...
Arrays January 01 ,2026

Merge Two Sorted Arr...

Find Missi...
Arrays January 01 ,2026

Find Missing Number...

Find Dupli...
Arrays January 01 ,2026

Find Duplicate Eleme...

Count Freq...
Arrays January 01 ,2026

Count Frequency of E...

Find the M...
Arrays January 01 ,2026

Find the Majority El...

Find All U...
Arrays January 01 ,2026

Find All Unique Elem...

Insert an...
Arrays January 01 ,2026

Insert an Element at...

Delete an...
Arrays January 01 ,2026

Delete an Element fr...

Find the I...
Arrays January 01 ,2026

Find the Index of an...

Find Union...
Arrays January 01 ,2026

Find Union of Two Ar...

Find Inter...
Arrays January 01 ,2026

Find Intersection of...

Sort an Ar...
Arrays January 01 ,2026

Sort an Array of 0s...

Find the L...
Arrays January 01 ,2026

Find the Largest Sum...

Kadane’s A...
Arrays January 01 ,2026

Kadane’s Algorithm (...

Two Sum Pr...
Arrays January 01 ,2026

Two Sum Problem

Subarray w...
Arrays January 01 ,2026

Subarray with Given...

Longest Su...
Arrays January 01 ,2026

Longest Subarray wit...

Rearrange...
Arrays January 01 ,2026

Rearrange Array Alte...

Leaders in...
Arrays January 01 ,2026

Leaders in an Array

Equilibriu...
Arrays January 01 ,2026

Equilibrium Index of...

Stock Buy...
Arrays January 01 ,2026

Stock Buy and Sell (...

Stock Buy...
Arrays January 01 ,2026

Stock Buy and Sell (...

Sort an Ar...
Arrays January 01 ,2026

Sort an Array of 0s,...

Find the M...
Arrays January 01 ,2026

Find the Majority El...

Find All P...
Arrays January 01 ,2026

Find All Pairs with...

Longest Co...
Arrays January 01 ,2026

Longest Consecutive...

Product of...
Arrays January 01 ,2026

Product of Array Exc...

Maximum Pr...
Arrays January 01 ,2026

Maximum Product Suba...

Find the F...
Arrays January 01 ,2026

Find the First Missi...

Count Inve...
Arrays January 01 ,2026

Count Inversions in...

Rearrange...
Arrays January 01 ,2026

Rearrange Array by S...

Check if A...
Arrays January 01 ,2026

Check if Array Can B...

Trapping R...
Arrays January 01 ,2026

Trapping Rain Water

Find Minim...
Arrays January 01 ,2026

Find Minimum in Rota...

Search in...
Arrays January 01 ,2026

Search in Rotated So...

Median of...
Arrays January 01 ,2026

Median of Two Sorted...

Merge Inte...
Arrays January 01 ,2026

Merge Intervals

Count Reve...
Arrays January 01 ,2026

Count Reverse Pairs

Longest Su...
Arrays January 01 ,2026

Longest Subarray wit...

Maximum Su...
Arrays January 01 ,2026

Maximum Sum Rectangl...

Subarray S...
Arrays January 01 ,2026

Subarray Sum Equals...

Count Dist...
Arrays January 01 ,2026

Count Distinct Eleme...

Sliding Wi...
Arrays January 01 ,2026

Sliding Window Maxim...

Find K Max...
Arrays January 01 ,2026

Find K Maximum Eleme...

Minimum Nu...
Arrays January 01 ,2026

Minimum Number of Ju...

Chocolate...
Arrays January 01 ,2026

Chocolate Distributi...

Find All T...
Arrays January 01 ,2026

Find All Triplets Wi...

Kth Smalle...
Arrays January 01 ,2026

Kth Smallest Element...

Maximum Le...
Arrays January 01 ,2026

Maximum Length Biton...

Find the S...
Arrays February 02 ,2026

Find the Second Larg...

Get In Touch

Kurki bazar Uttar Pradesh

+91-8808946970

techiefreak87@gmail.com