# Sum of f(a[i], a[j]) over all pairs in an array of n integers

Given an array of n integers, find the sum of f(a[i], a[j]) of all pairs (i, j) such that (1 <= i < j <= n).

** f(a[i], a[j]): **

If |a[j]-a[i]| > 1 f(a[i], a[j]) = a[j] - a[i] Else // if |a[j]-a[i]| <= 1 f(a[i], a[j]) = 0

**Examples:**

Input : 6 6 4 4 Output : -8 Explanation: All pairs are: (6 - 6) + (6 - 6) + (6 - 6) + (4 - 6) + (4 - 6) + (4 - 6) + (4 - 6) + (4 - 4) + (4 - 4) = -8 Input: 1 2 3 1 3 Output: 4 Explanation: the pairs that add up are: (3, 1), (3, 1) to give 4, rest all pairs according to condition gives 0.

A **naive approach** is to iterate through all pairs and calculate f(a[i], a[j]) and summing it while traversing in two nested loops will give us our answer.

**Time Complexity:** O(n^2)

A **efficient approach** will be to use a map/hash function to keep a count of every occurring numbers and then traverse through the list. While traversing through the list, we multiply the count of numbers that are before it and the number itself. Then subtract this result with the pre-sum of the number before that number to get the sum of difference of all pairs possible with that number. To remove all pairs whose absolute difference is <=1, simply subtract the count of occurrence of (number-1) and (number+1) from the previously computed sum. Here we subtract count of (number-1) from the computed sum as it had been previously added to the sum, and we add (number+1) count since the negative has been added to the pre-computed sum of all pairs.

**Time Complexity :** O(n)

**Below is the implementation of the above approach :**

## C++

`// CPP program to calculate the ` `// sum of f(a[i], aj]) ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate the sum ` `int` `sum(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// map to keep a count of occurrences ` ` ` `unordered_map<` `int` `, ` `int` `> cnt; ` ` ` ` ` `// Traverse in the list from start to end ` ` ` `// number of times a[i] can be in a pair and ` ` ` `// to get the difference we subtract pre_sum. ` ` ` `int` `ans = 0, pre_sum = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `ans += (i * a[i]) - pre_sum; ` ` ` `pre_sum += a[i]; ` ` ` ` ` `// if the (a[i]-1) is present then ` ` ` `// subtract that value as f(a[i], a[i]-1)=0 ` ` ` `if` `(cnt[a[i] - 1]) ` ` ` `ans -= cnt[a[i] - 1]; ` ` ` ` ` `// if the (a[i]+1) is present then ` ` ` `// add that value as f(a[i], a[i]-1)=0 ` ` ` `// here we add as a[i]-(a[i]-1)<0 which would ` ` ` `// have been added as negative sum, so we add ` ` ` `// to remove this pair from the sum value ` ` ` `if` `(cnt[a[i] + 1]) ` ` ` `ans += cnt[a[i] + 1]; ` ` ` ` ` `// keeping a counter for every element ` ` ` `cnt[a[i]]++; ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 1, 2, 3, 1, 3 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `cout << sum(a, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to calculate ` `// the sum of f(a[i], aj]) ` `import` `java.util.*; ` `public` `class` `GfG { ` ` ` ` ` `// Function to calculate the sum ` ` ` `public` `static` `int` `sum(` `int` `a[], ` `int` `n) ` ` ` `{ ` ` ` `// Map to keep a count of occurrences ` ` ` `Map<Integer,Integer> cnt = ` `new` `HashMap<Integer,Integer>(); ` ` ` ` ` `// Traverse in the list from start to end ` ` ` `// number of times a[i] can be in a pair and ` ` ` `// to get the difference we subtract pre_sum ` ` ` `int` `ans = ` `0` `, pre_sum = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `ans += (i * a[i]) - pre_sum; ` ` ` `pre_sum += a[i]; ` ` ` ` ` `// If the (a[i]-1) is present then subtract ` ` ` `// that value as f(a[i], a[i]-1) = 0 ` ` ` `if` `(cnt.containsKey(a[i] - ` `1` `)) ` ` ` `ans -= cnt.get(a[i] - ` `1` `); ` ` ` ` ` `// If the (a[i]+1) is present then ` ` ` `// add that value as f(a[i], a[i]-1)=0 ` ` ` `// here we add as a[i]-(a[i]-1)<0 which would ` ` ` `// have been added as negative sum, so we add ` ` ` `// to remove this pair from the sum value ` ` ` `if` `(cnt.containsKey(a[i] + ` `1` `)) ` ` ` `ans += cnt.get(a[i] + ` `1` `); ` ` ` ` ` `// keeping a counter for every element ` ` ` `if` `(cnt.containsKey(a[i])) { ` ` ` `cnt.put(a[i], cnt.get(a[i]) + ` `1` `); ` ` ` `} ` ` ` `else` `{ ` ` ` `cnt.put(a[i], ` `1` `); ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `a[] = { ` `1` `, ` `2` `, ` `3` `, ` `1` `, ` `3` `}; ` ` ` `int` `n = a.length; ` ` ` `System.out.println(sum(a, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Swetank Modi ` |

*chevron_right*

*filter_none*

## Python3

# Python3 program to calculate the

# sum of f(a[i], aj])

# Function to calculate the sum

def sum(a, n):

# map to keep a count of occurrences

cnt = dict()

# Traverse in the list from start to end

# number of times a[i] can be in a pair and

# to get the difference we subtract pre_sum.

ans = 0

pre_sum = 0

for i in range(n):

ans += (i * a[i]) – pre_sum

pre_sum += a[i]

# if the (a[i]-1) is present then

# subtract that value as f(a[i], a[i]-1)=0

if (a[i] – 1) in cnt:

ans -= cnt[a[i] – 1]

# if the (a[i]+1) is present then add that

# value as f(a[i], a[i]-1)=0 here we add

# as a[i]-(a[i]-1)<0 which would have been
# added as negative sum, so we add to remove
# this pair from the sum value
if (a[i] + 1) in cnt:
ans += cnt[a[i] + 1]
# keeping a counter for every element
if a[i] not in cnt:
cnt[a[i]] = 0
cnt[a[i]] += 1
return ans
# Driver Code
if __name__ == '__main__':
a = [1, 2, 3, 1, 3]
n = len(a)
print(sum(a, n))
# This code is contributed by
# SHUBHAMSINGH10
[tabby title="C#"]

`using` `System; ` `using` `System.Collections.Generic; ` ` ` `// C# program to calculate ` `// the sum of f(a[i], aj]) ` `public` `class` `GfG ` `{ ` ` ` ` ` `// Function to calculate the sum ` ` ` `public` `static` `int` `sum(` `int` `[] a, ` `int` `n) ` ` ` `{ ` ` ` `// Map to keep a count of occurrences ` ` ` `IDictionary<` `int` `, ` `int` `> cnt = ` `new` `Dictionary<` `int` `, ` `int` `>(); ` ` ` ` ` `// Traverse in the list from start to end ` ` ` `// number of times a[i] can be in a pair and ` ` ` `// to get the difference we subtract pre_sum ` ` ` `int` `ans = 0, pre_sum = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `ans += (i * a[i]) - pre_sum; ` ` ` `pre_sum += a[i]; ` ` ` ` ` `// If the (a[i]-1) is present then subtract ` ` ` `// that value as f(a[i], a[i]-1) = 0 ` ` ` `if` `(cnt.ContainsKey(a[i] - 1)) ` ` ` `{ ` ` ` `ans -= cnt[a[i] - 1]; ` ` ` `} ` ` ` ` ` `// If the (a[i]+1) is present then ` ` ` `// add that value as f(a[i], a[i]-1)=0 ` ` ` `// here we add as a[i]-(a[i]-1)<0 which would ` ` ` `// have been added as negative sum, so we add ` ` ` `// to remove this pair from the sum value ` ` ` `if` `(cnt.ContainsKey(a[i] + 1)) ` ` ` `{ ` ` ` `ans += cnt[a[i] + 1]; ` ` ` `} ` ` ` ` ` `// keeping a counter for every element ` ` ` `if` `(cnt.ContainsKey(a[i])) ` ` ` `{ ` ` ` `cnt[a[i]] = cnt[a[i]] + 1; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `cnt[a[i]] = 1; ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(` `string` `[] args) ` ` ` `{ ` ` ` `int` `[] a = ` `new` `int` `[] {1, 2, 3, 1, 3}; ` ` ` `int` `n = a.Length; ` ` ` `Console.WriteLine(sum(a, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Shrikant13 ` |

*chevron_right*

*filter_none*

**Output :**

4

## Recommended Posts:

- Minimum product of k integers in an array of positive Integers
- Find the maximum cost of an array of pairs choosing at most K pairs
- Given an array of pairs, find all symmetric pairs in it
- Find pairs in array whose sums already exist in array
- Factorial of an Array of integers
- Converting an array of integers into Zig-Zag fashion!
- Print all the peaks and troughs in an Array of Integers
- Longest sequence of positive integers in an array
- Find the first repeating element in an array of integers
- Check if array contains contiguous integers with duplicates allowed
- Find a pair with maximum product in array of Integers
- Move all zeros to start and ones to end in an Array of random integers
- Minimum increments to convert to an array of consecutive integers
- Given an array and two integers l and r, find the kth largest element in the range [l, r]
- Minimum product pair an array of positive Integers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.