WEAKMID - why this code has WA?

,

import java.util.ArrayList;
import java.util.Scanner;
import java.util.*;

class Weakmid {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
        int numberOfCases = in.nextInt();

        for(int i=0;i<numberOfCases;i++)
        {
        	int N  = in.nextInt();
        	ArrayList<Entry> l = new ArrayList<Entry>();
        	
        	for(int j=0;j<N;j++)
        	{
        		int t = in.nextInt();
        		Entry e = new Entry();
        		e.value=t;
        		e.noOfDaysBeforeStackTop=0;
        		e.indx=j;
        		l.add(e);
        	}
        	
        	Stack<Entry> st = new Stack();
        	
        	int day = 0;
        	
        	int[] result  = new int [N];
        	
        	for(Entry e:l)
        	{
        		if(st.isEmpty() || st.size()==1)
        			st.push(e);
        		else
        		{
        			if(st.peek().value<e.value && st.get(st.size()-2).value>st.peek().value)
        			{
        				do{
        				int removalDay = Integer.max(st.get(st.size()-2).noOfDaysBeforeStackTop, day) + 1;
        				result[st.pop().indx] = removalDay;
        				st.peek().noOfDaysBeforeStackTop =  removalDay;
        				day++;
        				}while(st.size()>2 && (st.peek().value<e.value && st.get(st.size()-2).value>st.peek().value));
        				
        				st.push(e);
        				day=0;
        			}
        			else
        			{
        				st.peek().noOfDaysBeforeStackTop=0;
        				st.push(e);
        				day=0;
        			}
        		}
        	}
        	
        	for(Entry e1:st)
        		result[e1.indx]=0;
        	
        	for(int t=0;t<result.length-1;t++)
        		System.out.print(result[t] + " ");
        	
        	System.out.print(result[result.length-1]);
        	
        	System.out.println("\n");
        	
        }
                    

        

	}
	
	static class Entry{
		int value;
		int indx;
		int noOfDaysBeforeStackTop;
		
	}

}

I found out the problem. I was not correctly tracking the day on which the current element being considered became adjacent to the element on stack top. Specifically, it was failing the case

1
10
100 90 80 70 95 10 20 30 60 100

for which the output should be

0 3 2 1 5 1 2 3 4 0 but I was getting
0 3 2 1 4 1 2 3 4 0

The correct code is

import java.util.ArrayList;
import java.util.Scanner;
import java.util.*;

class Weakmid {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
        int numberOfCases = in.nextInt();

        for(int i=0;i<numberOfCases;i++)
        {
        	int N  = in.nextInt();
        	ArrayList<Entry> l = new ArrayList<Entry>();
        	
        	for(int j=0;j<N;j++)
        	{
        		int t = in.nextInt();
        		Entry e = new Entry();
        		e.value=t;
        		e.noOfDaysBeforeStackTop=0;
        		e.indx=j;
        		l.add(e);
        	}
        	
        	Stack<Entry> st = new Stack();
        	
        	int dayTheElementisAdjacentToCurrentStackTop = 0;
        	
        	int[] result  = new int [N];
        	
        	for(Entry e:l)
        	{
        		if(st.isEmpty() || st.size()==1)
        			st.push(e);
        		else
        		{
        			//if the stack top is less then the adjacent two elements
        			if(st.peek().value<e.value && st.get(st.size()-2).value>st.peek().value)
        			{
        				do{
	        				int removalDay = Integer.max(st.get(st.size()-2).noOfDaysBeforeStackTop,dayTheElementisAdjacentToCurrentStackTop) + 1;
	        				result[st.pop().indx] = removalDay; //save the result
	        				st.peek().noOfDaysBeforeStackTop =  removalDay;
	        				dayTheElementisAdjacentToCurrentStackTop = removalDay;
	        			}while(st.size()>=2 && (st.peek().value<e.value && st.get(st.size()-2).value>st.peek().value)); //continue as long as the new stack top satisfies the condition
        				
        				dayTheElementisAdjacentToCurrentStackTop=0; //once the current element becomes stack top, for the next element dayTheElementisAdjacentToCurrentStackTop is 0
        				st.push(e);
        				
        			} //the stack top remains as it is with the current element
        			else
        			{
        				st.peek().noOfDaysBeforeStackTop=0;
        				st.push(e);
        			}
        		}
        	}
        	
        	for(Entry e1:st)
        		result[e1.indx]=0;
        	
        	for(int t=0;t<result.length-1;t++)
        		System.out.print(result[t] + " ");
        	
        	System.out.print(result[result.length-1]);
        	
        	System.out.println("\n");
        	
        }
                    

        

	}
	
	static class Entry{
		int value;
		int indx;
		int noOfDaysBeforeStackTop;
		
	}

}