Help me in solving STABARR problem

My issue

for input 1 2 1 2 why answer was 1 ,actually it will change both 1’s to 2 ??

My code

# cook your dish here
t=int(input())
while(t):
    t-=1
    n=int(input())
    l=list(map(int,input().split()))
    stack=[]
    ans=0
    for i in range(n-1,-1,-1):
        if len(stack)==0:
            stack.append(l[i])
        else:
            if l[i]>=stack[-1]:
                stack.pop()
                stack.append(l[i])
            else:
                ans+=1
    print(ans)

Problem Link: Stable Array Practice Coding Problem

at t=0 arr= 1 , 2 , 1 ,2
at t =1 arr= 2 ,2 ,2 ,2
time taken to become stable is 1 sec

at t=0 1,2,1,2
at t=1 2,2,1,2
at t=2 2,2,2,2 is this not correct way to understand the problem?

image
this means every second
for(int i=0;i<n-1;i++){
if(arr[i]<arr[i+1])arr[i]=arr[i+1];
}

1 Like

thank you