- Arrays, frequency-counting, Hash, HashSet, Mathematical, Searching

Numbers of pairs from an array whose average is also present in the array

  import java.io.*;import java.util.*;  class GFG {              static int getCountPairs(int arr[],                             int N, int S)    {        HashMap mp            = new HashMap();                          for (int i = 0; i < N; i++) {                                      if (!mp.containsKey(arr[i]))                mp.put(arr[i], 0);              mp.put(arr[i],                   mp.get(arr[i]) + 1);        }                          int twice_count = 0;                          for (int i = 0; i < N; i++) {                                      if (mp.get(S - arr[i])                != null) {                                  twice_count += mp.get(                    S - arr[i]);            }              if (S - arr[i] == arr[i])                twice_count--;        }                  return twice_count / 2;    }              public static int countPairs(        int arr[], int N)    {                        int count = 0;                  HashSet S = new HashSet();                          for (int i = 0; i < N; i++)            S.add(arr[i]);          for (int ele : S) {              int sum = 2 * ele;                                      count += getCountPairs(                arr, N, sum);        }                          return count;    }          public static void main(String[] args)    {        int N = 6;        int arr[] = { 4, 2, 5, 1, 3, 5 };        System.out.println(            countPairs(arr, N));    }}