title: Sum Fai Aj Pairs Array N Integers
signature: sum_fai_aj_pairs_array_n_integers <- function(a, n) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(a, n, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(a, n), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(2L, 8L, 12L, 19L, 23L, 23L, 26L, 39L, 54L, 56L, 57L, 57L, 73L, 78L, 83L, 83L, 89L, 91L), 15L, 
          3278L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(62L, -34L, 10L, -28L, -42L, -12L, 4L, 20L, -20L, -84L, -76L, -16L, -44L, 26L, -78L, -40L, 50L, -10L, -56L, 76L, -88L, 24L, 64L, 10L, 64L, -8L, -68L, -42L, 26L, 24L, 62L, 36L, -68L, 8L, -68L, -2L, 8L, 38L, -18L), 20L, 
          -112L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 18L, 
          0L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(23L, 14L, 44L, 29L, 93L, 56L, 22L, 29L, 97L, 71L, 43L, 72L, 74L, 8L, 92L, 40L, 18L, 34L, 78L, 79L, 93L, 63L, 79L, 44L, 35L, 72L, 88L, 83L, 40L, 89L, 66L, 66L, 54L, 56L, 44L, 62L, 72L, 94L, 79L, 79L, 24L, 55L, 72L, 37L, 27L, 55L, 16L, 58L, 83L), 25L, 
          2551L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-96L, -94L, -90L, -90L, -86L, -82L, -80L, -80L, -58L, -48L, -40L, -32L, -32L, -20L, -20L, -20L, -12L, -12L, -6L, -6L, -2L, 0L, 4L, 16L, 16L, 16L, 42L, 48L, 58L, 64L, 68L, 76L, 76L, 76L, 78L, 80L, 82L, 88L, 88L, 88L, 92L, 94L, 96L, 98L), 27L, 
          16640L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L), 34L, 
          0L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(3L, 4L, 4L, 8L, 10L, 10L, 11L, 15L, 19L, 19L, 20L, 24L, 25L, 25L, 27L, 30L, 41L, 43L, 44L, 45L, 47L, 55L, 59L, 59L, 61L, 63L, 63L, 67L, 67L, 69L, 72L, 73L, 75L, 77L, 77L, 78L, 81L, 81L, 83L, 84L, 92L, 94L, 99L), 31L, 
          12408L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(94L, -86L, 94L, 54L, -52L, 86L, 68L, 64L, 98L, 54L, -14L, -60L, -60L, -92L, 80L, -16L, 28L, 16L, -74L, 68L, 32L, -54L, 58L, -16L, -2L, -52L, -92L, -36L, 96L, -18L, 14L, 76L, 16L), 26L, 
          -6856L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L), 7L, 
          0L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(10L, 28L, 63L, 2L, 78L, 12L, 51L, 82L, 89L, 65L, 99L), 8L, 
          542L
      )))
  
  }
  
  main()
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;
  }