HISTOGRAM stacks and queues

import java.util.Stack;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int N = sc.nextInt();
int[] hist = new int[N];
for(int i=0;i<N;i++) {
hist[i] = sc.nextInt();
}

	Stack<Integer> stack = new Stack<>();
	int maxArea = 0;
	int area = 0;
	stack.push(0);
	for(int i=1;i<=N;i++) {
	 
	 
	 if(hist[i]>hist[stack.peek()]) {
		 stack.push(i);
	 }
	 
	 else {
		 int top = stack.pop();
		 if(stack.isEmpty()) {
		 area = hist[top]*(i);
				 }
		 else {
			 area = hist[top]*(i-stack.peek()-1);
		 }
	 }
	 
	 if(area>maxArea) {
		 maxArea = area;
	 }
	}
	
	System.out.println(maxArea);
	
}
}

//I’m getting empty stack exception

It is because you aren’t checking if stack is empty before peeking and popping.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.