fork(1) 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. static void process(int[] array) {
  11. int n = array.length;
  12. int[] maxLeft = new int[n];
  13.  
  14. maxLeft[0] = array[0];
  15. for (int i = 1; i < n; ++i) {
  16. maxLeft[i] = Math.max(maxLeft[i-1], array[i]);
  17. }
  18.  
  19. int minRight = array[array.length-1];
  20. for (int i = n-2; i >= 0; --i) {
  21. if (maxLeft[i] < minRight) {
  22. System.out.println("index = " + i + ", element = " + array[i]);
  23. return;
  24. }
  25. minRight = Math.min(minRight, array[i]);
  26. }
  27.  
  28. }
  29.  
  30. public static void main (String[] args) throws java.lang.Exception
  31. {
  32. int[] array1 = { 5, -2, 3, 8, 6 };
  33. int[] array2 = { 2, 1, 3, 6, 5 };
  34. process(array1);
  35. process(array2);
  36. }
  37. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
index = 2, element = 3
index = 2, element = 3