fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4.  
  5. public static int[] findCriticalPoints(int[] values) {
  6. int n = values.length;
  7. if (n < 3) return new int[]{-1, -1};
  8.  
  9. List<Integer> criticalIndices = new ArrayList<>();
  10.  
  11. for (int i = 1; i < n - 1; i++) {
  12. if ((values[i] > values[i - 1] && values[i] > values[i + 1]) ||
  13. (values[i] < values[i - 1] && values[i] < values[i + 1])) {
  14. criticalIndices.add(i);
  15. }
  16. }
  17.  
  18. if (criticalIndices.size() < 2) return new int[]{-1, -1};
  19.  
  20. int minDist = Integer.MAX_VALUE;
  21. for (int i = 1; i < criticalIndices.size(); i++) {
  22. int diff = criticalIndices.get(i) - criticalIndices.get(i - 1);
  23. minDist = Math.min(minDist, diff);
  24. }
  25.  
  26. int maxDist = criticalIndices.get(criticalIndices.size() - 1) - criticalIndices.get(0);
  27. return new int[]{minDist, maxDist};
  28. }
  29.  
  30. public static void main(String[] args) {
  31. Scanner sc = new Scanner(System.in);
  32. int n = sc.nextInt();
  33. int[] values = new int[n];
  34. for (int i = 0; i < n; i++) {
  35. values[i] = sc.nextInt();
  36. }
  37. int[] result = findCriticalPoints(values);
  38. System.out.println(result[0] + " " + result[1]);
  39. }
  40. }
  41.  
Success #stdin #stdout 0.22s 58796KB
stdin
7
5 3 1 2 5 1 2
stdout
1 3