title: Sum Fai Aj Pairs Array N Integers
signature: function sum_fai_aj_pairs_array_n_integers(a, n)
test_code: 
  
  -- SOLUTION CODE
  -- ============================================
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  function isCorrect(actual, expected)
      if type(expected) ~= type(actual) then
          return false
      end
      
      if type(expected) == 'table' then
          for k,v_expected in pairs(expected) do
              
              if actual[k] == nil then
                  return false
              end
              local v_actual = actual[k]
              if not isCorrect(v_actual, v_expected) then
                  return false
              end
              actual[k]=nil
          end
      
          if next(actual) then 
              return false
          end
          return true
      else
          return actual == expected
      end
  end
  
  function validateSolution(actual, expected)
      return isCorrect(actual,expected)
  end
  
  function driver(a, n, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(a, n) 
      end)
      if status then
          if validateSolution(exec_result,expected) then
              return "PASSED"
          end
          return "FAILED"
      else
          return string.format("ERROR=%s",string.gsub(error_msg,".*:[%d]+: ",""))
      end
  end
      
  function main()
      result = driver({2, 8, 12, 19, 23, 23, 26, 39, 54, 56, 57, 57, 73, 78, 83, 83, 89, 91}, 15, 3278)
      print(string.format("TEST-0...%s",result))
  
      result = driver({62, -34, 10, -28, -42, -12, 4, 20, -20, -84, -76, -16, -44, 26, -78, -40, 50, -10, -56, 76, -88, 24, 64, 10, 64, -8, -68, -42, 26, 24, 62, 36, -68, 8, -68, -2, 8, 38, -18}, 20, -112)
      print(string.format("TEST-1...%s",result))
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 18, 0)
      print(string.format("TEST-2...%s",result))
  
      result = driver({23, 14, 44, 29, 93, 56, 22, 29, 97, 71, 43, 72, 74, 8, 92, 40, 18, 34, 78, 79, 93, 63, 79, 44, 35, 72, 88, 83, 40, 89, 66, 66, 54, 56, 44, 62, 72, 94, 79, 79, 24, 55, 72, 37, 27, 55, 16, 58, 83}, 25, 2551)
      print(string.format("TEST-3...%s",result))
  
      result = driver({-96, -94, -90, -90, -86, -82, -80, -80, -58, -48, -40, -32, -32, -20, -20, -20, -12, -12, -6, -6, -2, 0, 4, 16, 16, 16, 42, 48, 58, 64, 68, 76, 76, 76, 78, 80, 82, 88, 88, 88, 92, 94, 96, 98}, 27, 16640)
      print(string.format("TEST-4...%s",result))
  
      result = driver({1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1}, 34, 0)
      print(string.format("TEST-5...%s",result))
  
      result = driver({3, 4, 4, 8, 10, 10, 11, 15, 19, 19, 20, 24, 25, 25, 27, 30, 41, 43, 44, 45, 47, 55, 59, 59, 61, 63, 63, 67, 67, 69, 72, 73, 75, 77, 77, 78, 81, 81, 83, 84, 92, 94, 99}, 31, 12408)
      print(string.format("TEST-6...%s",result))
  
      result = driver({94, -86, 94, 54, -52, 86, 68, 64, 98, 54, -14, -60, -60, -92, 80, -16, 28, 16, -74, 68, 32, -54, 58, -16, -2, -52, -92, -36, 96, -18, 14, 76, 16}, 26, -6856)
      print(string.format("TEST-7...%s",result))
  
      result = driver({0, 0, 0, 0, 1, 1, 1, 1}, 7, 0)
      print(string.format("TEST-8...%s",result))
  
      result = driver({10, 28, 63, 2, 78, 12, 51, 82, 89, 65, 99}, 8, 542)
      print(string.format("TEST-9...%s",result))
  
  end
  
  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;
  }