title: Sum Fai Aj Pairs Array N Integers
signature: sumFaiAjPairsArrayNIntegers(a, n) {
source_py: def sum_fai_aj_pairs_array_n_integers(a, n):
      cnt = dict()
      ans = 0
      pre_sum = 0
      for i in range(n):
          ans += ((i * a[i]) - pre_sum)
          pre_sum += a[i]
          if ((a[i] - 1) in cnt):
              ans -= cnt[(a[i] - 1)]
          if ((a[i] + 1) in cnt):
              ans += cnt[(a[i] + 1)]
          if (a[i] not in cnt):
              cnt[a[i]] = 0
          cnt[a[i]] += 1
      return ans
source_cpp: int sum_fai_aj_pairs_array_n_integers(vector<int> a, int n) {
    unordered_map < int, int > cnt;
    int ans = 0, pre_sum = 0;
    for ( int i = 0;
    i < n;
    i ++ ) {
      ans += ( i * a [ i ] ) - pre_sum;
      pre_sum += a [ i ];
      if ( cnt [ a [ i ] - 1 ] ) ans -= cnt [ a [ i ] - 1 ];
      if ( cnt [ a [ i ] + 1 ] ) ans += cnt [ a [ i ] + 1 ];
      cnt [ a [ i ] ] ++;
    }
    return ans;
  }