/* package whatever; // don't place package name! */
import java.util.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
int[]arr={3,1,4,1,5};
Stack<Integer>st=new Stack<>();
//3,1
ArrayList<Integer>arraylist=new ArrayList<>();
for(int i=0;i<arr.length;i++){
if(st.isEmpty()){
arraylist.add(-1);
st.push(i);
continue;
}if(arr[st.peek()]>arr[i]){
arraylist.add(st.peek());
continue;
}
if(arr[st.peek()]<=arr[i]){
st.pop();
st.push(i);
arraylist.add(-1);
}
}
int ans=0;
for(int i=0;i<arraylist.size();i++){
ans
=Math.
max(i
-arraylist.
get(i
)-1,ans
); }
System.
out.
println(arraylist
); }
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwoKLyogTmFtZSBvZiB0aGUgY2xhc3MgaGFzIHRvIGJlICJNYWluIiBvbmx5IGlmIHRoZSBjbGFzcyBpcyBwdWJsaWMuICovCmNsYXNzIElkZW9uZQp7CglwdWJsaWMgc3RhdGljIHZvaWQgbWFpbiAoU3RyaW5nW10gYXJncykgdGhyb3dzIGphdmEubGFuZy5FeGNlcHRpb24KCXsKCQkgaW50W11hcnI9ezMsMSw0LDEsNX07CgkJIFN0YWNrPEludGVnZXI+c3Q9bmV3IFN0YWNrPD4oKTsKCQkgLy8zLDEKCQkgQXJyYXlMaXN0PEludGVnZXI+YXJyYXlsaXN0PW5ldyBBcnJheUxpc3Q8PigpOwoJCSBmb3IoaW50IGk9MDtpPGFyci5sZW5ndGg7aSsrKXsKCQkgCWlmKHN0LmlzRW1wdHkoKSl7CgkJIAkKCQkgCQlhcnJheWxpc3QuYWRkKC0xKTsKCQkgCQkJc3QucHVzaChpKTsKCQkgCQljb250aW51ZTsKCQkgCX1pZihhcnJbc3QucGVlaygpXT5hcnJbaV0pewoJCSAJCWFycmF5bGlzdC5hZGQoc3QucGVlaygpKTsgICAgIAoJCSAJCWNvbn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