fork download
  1.  
  2. import java.util.HashMap;
  3.  
  4. public class Main {
  5. public static void main(String[] args) {
  6. int n = 6;
  7. int[] P = new int[]{0,6,7,2,100,10};
  8.  
  9.  
  10. long c = 0;
  11. HashMap<Integer, Integer> freq = new HashMap<>();
  12.  
  13.  
  14. if (n > 0) {
  15. freq.put(P[0], 1);
  16. }
  17.  
  18.  
  19. for (int j = 1; j < n; j++) {
  20. int rhs = P[j] - j;
  21. c = c + freq.getOrDefault(rhs, 0);
  22.  
  23. int prevValue= P[j - 1] - j + 1;
  24. freq.put(prevValue, freq.getOrDefault(prevValue, 0) + 1);
  25. }
  26.  
  27. System.out.println(c);
  28.  
  29. }
  30. }
  31.  
Success #stdin #stdout 0.07s 54552KB
stdin
Standard input is empty
stdout
2