maximum area in the histogram

 import java.util.*;


public class MaxAreaInHistogram {
    public static void MaxArea(int arr[]) {
        Stack<Integer> s = new Stack<>();
        int nsr[] = new int[arr.length];
        int nsl[] = new int[arr.length];
        int maxArea = 0;
        // INDEX OF NEXT SMALL RIGHT
        for (int i = arr.length - 1; i >= 0; i--) {
            while (!s.isEmpty() && arr[s.peek()] >= arr[i]) {
                s.pop();
            }
            if (s.isEmpty()) {
                nsr[i] = arr.length;
            } else {
                nsr[i] = s.peek();
            }
            s.push(i);
        }
        // INDEX OF NEXT SMALL LEFT
        s = new Stack<>();
        for (int i = 0; i < arr.length; i++) {
            while (!s.isEmpty() && arr[s.peek()] >= arr[i]) {
                s.pop();
            }
            if (s.isEmpty()) {
                nsl[i] = -1;
            } else {
                nsl[i] = s.peek();
            }
            s.push(i);
        }
        // MAXIMUM AREA
        for (int i = 0; i < arr.length; i++) {
            int height = arr[i];
            int width = nsr[i] - nsl[i] - 1;
            int currArea = height * width;
            maxArea = Math.max(maxArea, currArea);

        }
        System.out.println("Maximum Area in the histogram: " + maxArea);

    }

    public static void main(String[] args) {
        int arr[] = { 2, 1, 5, 6, 2, 3 };
        MaxArea(arr);
    }
}

Comments