Samkit5025

Untitled

Jun 15th, 2022
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Solution {
  4.  
  5. static Scanner sc = new Scanner(System.in);
  6.  
  7. public static int pairs(int n) {
  8. return (n * (n-1))/2;
  9. }
  10.  
  11. public static int completeAssignment(int N,int[] A){
  12. Map<Integer,Integer> hm = new HashMap<Integer,Integer>();
  13.  
  14. for(int i=0;i<N;i++) {
  15. A[i]+=i;
  16. hm.put(A[i], hm.getOrDefault(A[i], 0) + 1);
  17. }
  18. int res =0;
  19.  
  20. for(int i : hm.values()) {
  21. res+=pairs(i);
  22. }
  23. return res;
  24. }
  25.  
  26. public static void main(String[] args) {
  27. int N = sc.nextInt();
  28. int[] A = new int[N];
  29.  
  30. for(int i=0;i<N;i++) {
  31. A[i] = sc.nextInt();
  32. }
  33.  
  34. System.out.println(completeAssignment(N,A));
  35. }
  36. }
  37.  
  38.  
  39.  
  40.  
Advertisement
Add Comment
Please, Sign In to add comment