fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4.  
  5. /* Name of the class has to be "Main" only if the class is public. */
  6. class Ideone
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. int[]arr={3,1,4,1,5};
  11. Stack<Integer>st=new Stack<>();
  12. //3,1
  13. ArrayList<Integer>arraylist=new ArrayList<>();
  14. for(int i=0;i<arr.length;i++){
  15. if(st.isEmpty()){
  16.  
  17. arraylist.add(-1);
  18. st.push(i);
  19. continue;
  20. }if(arr[st.peek()]>arr[i]){
  21. arraylist.add(st.peek());
  22. continue;
  23. }
  24. if(arr[st.peek()]<=arr[i]){
  25. st.pop();
  26. st.push(i);
  27. arraylist.add(-1);
  28.  
  29. }
  30. }
  31. int ans=0;
  32. for(int i=0;i<arraylist.size();i++){
  33. ans=Math.max(i-arraylist.get(i)-1,ans);
  34. }
  35. System.out.println(arraylist);
  36. }
  37. }
Success #stdin #stdout 0.11s 54528KB
stdin
Standard input is empty
stdout
[-1, 0, -1, 2, -1]