FastPrepMaximum Capped Histogram Area After One Removal
Problems/Maximum Capped Histogram Area After One Removal

Maximum Capped Histogram Area After One Removal

Myntra logoMyntra● MediumINTERNOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report names a Largest Rectangle in Histogram variant with one required building removal and an area cap, but omits deletion and cap semantics, bounds, and examples. The judged core task matches the report at about 88%.

You are given positive building heights heights and a positive area limit limit. Remove exactly one building; the buildings on its two sides become adjacent.

For the remaining histogram, compute its ordinary largest axis-aligned rectangle area. A removal is eligible only when that largest area is at most limit. Return the greatest eligible largest-rectangle area over all removals. If no removal is eligible, return 0.

Function

maxCappedAreaAfterRemoval(heights: long[], limit: long) → long

Examples

Example 1

heights = [2,1,5,6,2,3]limit = 10return = 10

Removing the height 1 leaves [2, 5, 6, 2, 3], whose largest rectangle has area 10. It is eligible and no eligible removal produces a larger area.

Example 2

heights = [2,4,2]limit = 6return = 4

Every possible removal leaves a two-building or one-building histogram whose largest rectangle area is 4.

Example 3

heights = [3,3,3]limit = 4return = 0

After any removal, the two remaining buildings form a largest rectangle of area 6, so no removal is eligible.

Constraints

  • 1 <= heights.length <= 2000.
  • 1 <= heights[i] <= 10^9.
  • 1 <= limit <= 10^18.

More Myntra problems

  • Detect a Server RAM Capacity BreachONSITE INTERVIEW · Seen May 2026
  • Minimum Route Reversals to Warehouse ZeroONSITE INTERVIEW · Seen May 2026
  • Minimum Swaps to Group Values at Most KONSITE INTERVIEW · Seen May 2026
All Myntra interview questionsSee Myntra hiring insights
Saved
class Solution {
    public long maxCappedAreaAfterRemoval(long[] heights, long limit) {
        // Return the best eligible largest-rectangle area.
    }
}
Checking account…