fork(3) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. //int[] A={86,34, 8, 10, 3, 2, 80, 30, 33, 1};
  13. //int[] A={9, 2, 3, 4, 5, 6, 7, 8, 18, 0};
  14. //int[] A={1, 2, 3, 4, 5, 6};
  15. //int[] A={6, 5, 4, 3, 2, 1};
  16. //int[] A={1, 5, 4, 6, 10, 7, 8};
  17. //int[] A={9, 2, 3, 4, 5, 6, 7, 18, 1, 0};
  18. int[] A={99, 100 , 1, 2, 3, 4, 5, 6, 7};
  19. System.out.print("Result max index is "+getMaxIndexDiff(A));
  20. }
  21. public static int getMaxIndexDiff(int[] A){
  22. int max=0;
  23. int i,j;
  24. int[] stack=new int[A.length];
  25. int top=-1;
  26. j=A.length-1;
  27. for(i=A.length-2;i>=0;i--){
  28. if(A[i]>A[j]){
  29. stack[++top]=j;
  30. j=i;
  31. }
  32. }
  33. i++;
  34. if(j-i>max)
  35. max=j-i;
  36.  
  37. i=0;
  38. while(top>=0 && i<A.length){
  39. if(A[i]<A[stack[top]]){
  40. j=stack[top--];
  41. if(j-i>max)
  42. max=j-i;
  43. }
  44. else
  45. i++;
  46. }
  47.  
  48. return max;
  49. }
  50. }
Success #stdin #stdout 0.06s 380160KB
stdin
Standard input is empty
stdout
Result max index is 6