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).
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
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 :
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Find the minimum value of the given expression over all pairs of the array
- Find the maximum possible value of a[i] % a[j] over all pairs of i and j
- Minimum product of k integers in an array of positive Integers
- Given an array of pairs, find all symmetric pairs in it
- Count of all possible pairs of disjoint subsets of integers from 1 to N
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Calculate the Sum of GCD over all subarrays
- Iterating over all possible combinations in an Array using Bits
- Maximum set bit count from pairs of integers from 0 to N that yields a sum as N
- Sum of XOR of sum of all pairs in an array
- Median in a stream of integers (running integers)
- Mode in a stream of integers (running integers)
- Compute maximum of the function efficiently over all sub-arrays
- Find the maximum cost of an array of pairs choosing at most K pairs
- Find all the possible remainders when N is divided by all positive integers from 1 to N+1
- Count of pairs of integers whose difference of squares is equal to N
- Sum of Bitwise And of all pairs in a given array
- Sum of absolute differences of all pairs in a given array
- Sum of product of all pairs of array elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.